I have a bunch of TextBoxes in my WinForm UserControl. Each of those text boxes has few event handlers such as On_Enter - show ListBox with suggestions, On_KeyUP - if Keys.Code == Keys.Enter
- SelectNextControl(). When I place that control in a Form, none of those events fire up. How to expose all those events to the containing form? How to make UserControl's events fire up event handlers of that UserControl?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
So, if I understand correct, I think there are 2 ways you can proceed:
Approach 1
In the UserControl, set the Modifiers property of each textbox (or the ones you are interested in) to public:
Then in the Form that uses this UserControl you can access all these textboxes and hence their events:
Approach 2 (Taken from this post)
You can create new events for your UserControl that simply pass forward the event of an underlying textbox. The underlying textboxes can then remain private to the UserControl.
In the UserControl add this event:
Or you can create a single event that deals with all textboxes:
Now your UserControl has a event of its own that the code in the Form can use:
Note that while this approach keeps the textboxes private within the UserControl, they can still be accessed from the event handler by the
sender
argument.