"event bubbling" for winform control
-
Hi, I have a composite control which contains some other controls and I want to use "event bubbling" for my winform control. Is it possible for child control to forward event to Composite control? Any advise on this would be useful. Thanks
-
Hi, I have a composite control which contains some other controls and I want to use "event bubbling" for my winform control. Is it possible for child control to forward event to Composite control? Any advise on this would be useful. Thanks
Do you mean a child control that embedded in a composite control? Because if that's the case, it's possible to wire events such that the composite control recieves the event. Please post some code and I'll show you. Regards Senkwe Just another wannabe code junky
-
Do you mean a child control that embedded in a composite control? Because if that's the case, it's possible to wire events such that the composite control recieves the event. Please post some code and I'll show you. Regards Senkwe Just another wannabe code junky
Thanks for responding to my post. Unfortunately I can not post any code. I’ll try to be clearer about my question this time. I have a composite control, combination of standard control (say text box). Every time any onClick happens on this child(text box) control, I want to pass this event to parent(composite) control. -Kasturi
-
Thanks for responding to my post. Unfortunately I can not post any code. I’ll try to be clearer about my question this time. I have a composite control, combination of standard control (say text box). Every time any onClick happens on this child(text box) control, I want to pass this event to parent(composite) control. -Kasturi
There are a million articles online that deal with using delegates and events and these are the two topics you need to understand so you know what is going on. However, I found that most rticles are prtty poor when it comes to these topics. The article that I used to gain a good understnding i the following. http://msdn.microsoft.com/msdnmag/issues/01/08/net/net0108.asp I modified this code to be more generic, i.e. I have a class that works as a generic event "attacher" and "detacher". It's not that pretty though. I don't have the code with me right now, so you'd have to wait for me to whip it up again if you have the patience. Otherwise you an read the article above and just dig in. It's well written nd ver clear at least to me. I'll still whip up that sample of mine though. Regards Senkwe Just another wannabe code junky
-
Hi, I have a composite control which contains some other controls and I want to use "event bubbling" for my winform control. Is it possible for child control to forward event to Composite control? Any advise on this would be useful. Thanks
Here's a sample EventsManger class. All it does is check to see if there are any listeners for registered events. As you can see it's very simple.
public class EventsManager
{
public delegate void EventHandler(Object sender);
public event EventHandler EventsHolder;protected virtual void OnEventOccurred() { if(EventsHolder != null) //meaning there is a listener for the event somewhere EventsHolder(this); } public void EventWasFired() { OnEventOccurred(); }
}
I prefer to have my entry point in a class called driver (I'm used to C++) so I declare the following...
public class driver
{
//declare an events manager variable, make itpublic
static
so you can query it from all classes in your
//namespace
public static EventsManager em;static void Main() { em = new EventsManager(); Application.Run(new Form1()); }
}
To test it, I created a user control with a button on it. i.e. a composite control. Now, I want that when I press the button, the user control back color turns red, ie, the button_click event gets bubbled up to the parent control so to speak. To make things easy, you can make your user control take an
EventsManager
as a constructor paramater.Then register a listener like so...public UserControl1(EventsManager em)
{
InitializeComponent();
em.EventsHolder += new EventsManager.EventHandler(ButtonWasPressed);
}private void ButtonWasPressed(Object sender)
{
this.BackColor = Color.Red;
}You can then fire the event from the button's OnClick event like so
private void button1_Click(object sender, System.EventArgs e)
{
driver.em.EventWasFired();
}You'll notice that I don't use
EventArgs
in my delegate declaration. I left that out because it's of no real use here. But it's advisable fro you to do this. (I typically derive a class fromEventArgs
and stuff it with mty own event specific code) Well it's ugly but it works for me, hope it helps Just another wannabe code junky