Apply same event to every project button in a C#

前端 未结 2 1804
傲寒
傲寒 2021-01-27 09:34

I have a project with 3 forms and 10 user controls. Each of these components has around 10 buttons. I would like to use an event to apply a style when they are hovered by the u

2条回答
  •  囚心锁ツ
    2021-01-27 10:30

    Your best option probably would be to subclass the button class, and use that throughout the rest of your code. Reflecting through all your forms and user-defined controls to identify and add an event handler to each button will be costly (both in terms of developer time and run time). Since the Button class isn't sealed, you can easily override the OnMouseHover() method to update the button as needed (making sure to call the base class method before leaving your override).

提交回复
热议问题