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(Clo sePanelEve nt, value);
}
remove
{
this.Events.RemoveHandler( ClosePanel Event, value);
}
}
private void OnClosePanel(ClosePanelEve ntArgs args)
{
ClosePanelEventHandler handler = this.Events[ClosePanelEven t] 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(obj ect sender,ClosePanelEventArgs args);
----
private void addLightControlPanel(strin g 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(thi s.Panel_Cl osed);
LightControlPanels.Add(Lig htName, DynLCP);
this.uxDynLCPPnl.Controls. Add((Light ControlPan el)LightCo ntrolPanel s[LightNam e]);
}
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(obj ect 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(thi s.c1_Close Panel);
}
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...
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(Clo
}
remove
{
this.Events.RemoveHandler(
}
}
private void OnClosePanel(ClosePanelEve
{
ClosePanelEventHandler handler = this.Events[ClosePanelEven
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(obj
----
private void addLightControlPanel(strin
{
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(thi
LightControlPanels.Add(Lig
this.uxDynLCPPnl.Controls.
}
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(obj
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(thi
}
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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!