Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Registering an event to a custom Windows Control Libary

Status
Not open for further replies.

techsay

Technical User
Oct 29, 2008
4
US
I am trying to create a custom control to use in my application. The control has several panels each panel consisting of different labels. One panel has a button control in it. I am having trouble figuring out how to have my custom control give the end user a

private void button1_Click(object sender, EventArgs e)
{
<User places code here>

}

event to write code in. I do not want the code to be in the actual custom control. I want the end-user using my custom control to be able to add their own control to the button click event of the custom control. Just like the default toolbox button does.

After compiling my custom control and adding it to the toolbox and placing it on a winForm I am not able to even double click the custom control button to get a button_click event. Instead, I get a

private void userControl1_Load(object sender, EventArgs e)

I am new to writing custom controls. Does anyone have an example of how to add events to a custom control such has a button_click event when the end user clicks my custom control button only.

 
In your control add
Code:
public event EventHandler FromMyControl;
private void PassToContainer(object sender, EventArgs e)
{
   EventHandler tmp = FromMyControl;
   if (tmp != null)
      tmp(sender, e);
}

In the control event you wish to pass from the control
Code:
private void button1_Click(object sender, EventArgs e)
{
   PassToContainer(sender, e);
}

Drop your control on a form and look through the events. There will be an event named "FromMyControl". Double click on it and in will create the event in code.


If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Foada,

Simple. Worked great. Thanks alot
 
you can take this one step farther and remove the null check by supplying an empty handler to FromMyControl in the controls constructor. This is known as the Null Object pattern. it's a simple way to eliminate null checks.
Code:
class MyControl
{
   public event EventHandler FromMyControl;

   private readonly handler empty = delegate(object o, EventArgs e) { };

   public MyControl()
   {
      FromMyControl += empty;
   }

   public override void Dispose()
   {
       FromMyControl -= empty;
       base.Dispose();
   }

   private void PassToContainer(object sender, EventArgs e)
   {
      FromMyControl(sender, e);
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason is there an advantage to using the Null Object pattern in this example? I can't see it.

PS. That's not a criticism to your post, just wondering if I am missing something... :)
 
no need to check for null values. this also make the code more readable because you are removing the null check.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
The null check is to determine if there are any subscribers to the event. If there are no subscribers the event is not fired.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
exactly. you can remove the need for the null check by subscribing an empty event handler.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I wonder if one way performs better than the other?

If no-one knows I'll do a test and post my results.
 
it's not a matter of better or worse. it's a pattern. applied correctly it can reduce LOC and keep the code base maintainable and readable. for this scenario it's really just preference.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top