Author | Message | Time |
---|---|---|
iNsaNe | I have made custom buttons but I am having trouble handling the mouseclick event from the parent. How can I do this? | January 1, 2009, 2:29 AM |
dRAgoN | set your events in your user control to send event notices. | January 2, 2009, 12:37 PM |
Myndfyr | [quote author=iNsaNe link=topic=17765.msg180993#msg180993 date=1230776963] I have made custom buttons but I am having trouble handling the mouseclick event from the parent. How can I do this? [/quote] You can export events from your user control. For instance, if you have three buttons, named 'start', 'end', and 'pause', you might consider: [code] // this code should be within your User Control class: private void start_Click(object sender, EventArgs e) { OnStartClicked(e); } private void end_Click(object sender, EventArgs e) { OnEndClicked(e); } private void pause_Click(object sender, EventArgs e) { OnPauseClicked(e); } public event EventHandler StartClicked; protected virtual void OnStartClicked(EventArgs e) { if (StartClicked != null) StartClicked(this, e); } public event EventHandler EndClicked; protected virtual void OnEndClicked(EventArgs e) { if (EndClicked != null) EndClicked(this, e); } public event EventHandler PauseClicked; protected virtual void OnPauseClicked(EventArgs e) { if (PauseClicked != null) PauseClicked(this, e); } [/code] You can do some additional things on here to make your control more designer-friendly: [code] // at the head of the file using System.ComponentModel; // then at your event declarations: [Browsable(true)] [Category("Behavior")] [Description("Informs listeners that the control's Start button has been clicked.")] public event EventHandler StartClicked; // you can also specify a default event at your class declaration: [DefaultEvent("StartClicked")] public partial class MyControl : UserControl { [/code] Here's a breakdown of what's going on: 1.) Within your user control you should be handling the events from the buttons. The buttons belong to the user control (the user control contains them), and so they should be considered "implementation details" and therefore be encapsulated. That's why the user control has the start_Click, pause_Click, and end_Click event handlers. They should be registered as normal. 2.) Then, we define events using the .NET Event Usage Guidelines (you can read more about that here). The event should be encapsulated in a protected virtual call, OnXxx, so that descendant classes may override the behavior. The other nice thing about using that call is that it shields you from NullReferenceExceptions that may be thrown if you attempt to raise the event directly. 3.) Controls or Forms that then use your user control hook into the StartClicked, EndClicked, or PauseClicked events instead of directly to the buttons. This shields you from changed implementation details later - such as if you wanted to go to a radio button list or some other type of UI interface. The events will also appear in the designer (under the "Misc" category if you don't give them the Category attribute, or in the specified Category if you do). If you specify a DefaultEvent, when you double-click the control, that will be the event that gets code auto-generated for it. | January 3, 2009, 12:06 AM |