Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. "event bubbling" for winform control

"event bubbling" for winform control

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kasturirawat
    wrote on last edited by
    #1

    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

    S 2 Replies Last reply
    0
    • K kasturirawat

      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

      S Offline
      S Offline
      Senkwe Chanda
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • S Senkwe Chanda

        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

        K Offline
        K Offline
        kasturirawat
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • K kasturirawat

          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

          S Offline
          S Offline
          Senkwe Chanda
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • K kasturirawat

            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

            S Offline
            S Offline
            Senkwe Chanda
            wrote on last edited by
            #5

            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 it public 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 from EventArgs 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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups