Link to home
Start Free TrialLog in
Avatar of i-Thomas
i-Thomas

asked on

discussion: different methods of events handling

Hi experts!

In another thread I asked question about event handling and two possible ways appeared.

I have Class1, a custom control and Class2, my main Form app. The custom control is a panel with several controls and it contains a close button. I want to handle the event of the close button, publish it and in the main class I want to subscribe to the event. In the main class the custom control will be created during runtime and elements will be stored in a Hybrid dictionary.

Here are the two possible solutions and I would like to get your opinion (or see another simpler, more elegant or more effective solution) on what was suggested and what advantages / disadvantages each of the solution brings.

Solution 1:

private static readonly object ClosePanelEvent = new object();

        public event ClosePanelEventHandler ClosePanel
        {
            add
            {
                this.Events.AddHandler(ClosePanelEvent, value);
            }
           
            remove
            {
                this.Events.RemoveHandler(ClosePanelEvent, value);
            }
        }


        private void OnClosePanel(ClosePanelEventArgs args)
        {
            ClosePanelEventHandler handler = this.Events[ClosePanelEvent] as ClosePanelEventHandler;
            if (handler != null)
            {
                handler(this,args);
            }
        }


       private void uxCloseBtn_Click(object sender, System.EventArgs e)    // this triggers the event
       {
          /// just adding a value to show you how to do it
            OnClosePanel(new ClosePanelEventArgs(10)); /// note
       }


public class ClosePanelEventArgs : EventArgs   // Definition of the event class
{
     public readonly int ID;
     
     
     public ClosePanelEventArgs(int ID)    
     {
          this.ID = ID;
     }
     
     
}
   
   
    public delegate void ClosePanelEventHandler(object sender,ClosePanelEventArgs args);


----


private void addLightControlPanel(string LightName)
{
     int index = LightControlPanels.Count;
     LightControlPanel DynLCP = new LightControlPanel();
     DynLCP.lightControlName = LightName;
     DynLCP.BackColor = SystemColors.ControlLight;
     DynLCP.Location = new Point(10 + (index * 150),10);
     DynLCP.ClosePanel += new ClosePanelEventHandler(this.Panel_Closed);      
     
     LightControlPanels.Add(LightName, DynLCP);
     this.uxDynLCPPnl.Controls.Add((LightControlPanel)LightControlPanels[LightName]);
}


      private void Panel_Closed(object sender, ClosePanelEventArgs e)
        {
            // Convert the sender object to a specific object type.
            LightControlPanel panel = (LightControlPanel) sender;

            // Get the name of the panel.
            string name = panel.lightControlName;
            Debug.WriteLine("Panel " + name + " pressed");
        }


***********************

Solution 2:

public delegate void ClosePanelEventHandler(object sender, ClosePanelEventArgs e);

public class ClosePanelEventArgs : EventArgs
{ ... }

public class Class1
{
   public event ClosePanelEventHandler ClosePanel;

   private void OnClosePanel()
   {
      if(this.ClosePanel != null)
         this.ClosePanel(this, new ClosePanelEventArgs(...));
   }
}

public class Class2
{
   private void SomeMethod()
   {
      Class1 c1 = new Class1();
      c1.ClosePanel += new ClosePanelEventHandler(this.c1_ClosePanel);
   }

   private void c1_ClosePanel(object sender, ClosePanelEventArgs e)
   {
      // ...
   }
}


I really look forward to reading your experts discussion about this! Please don't forget explanations as I want to learn from this thread and get deeper understanding of events / delegates...

Avatar of cyberdevil67
cyberdevil67

Hi i-Thomas,

 Solution 2 is the solution that I would end up with, however Solution 1 was not very clear as to which class each bit of code went into.

 Here is how I would do it, and as stated it is along the lines of Solution 2.

 When closing the CustomControl, do all the necessary cleanup etc. So you open the class as per normal, then fire an event back to the main class to say that the customcontrol has closed. Then you can have your main class do whatever it is it wants to do. And although you don't say it but I am assuming the custom control has no window properties, if it does then you will need to also do a custom closing event when the bigred x has been hit as well.

 Personally I look at solution 2 and its more clearer as to what is going on.

Cheers!
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah Sorry my post should have been Solution 1 as well.

As b1xml2 has already stated, about the creation or instantiation of a control it makes more sense to do it the way solution 1 does it.