473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Delegate and Event Handler

Hi All,

I saw a sample code and I think it makes life more complicated with
delegate and event handler stuff. Please correct me if I were wrong.

I have a method defined as below:

private void POToolBar_ButtonClick (object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (e.Button.Tag.ToString())
{
case "Refresh":
DataRefreshMenuItem.PerformClick(); // This calls
the delegate
break;
case "Add":
NewOrderItemButton.PerformClick();
break;
case "Save":
SaveOrderButton.PerformClick();
break;
}
}

Then in the debugger, when executing
"DataRefreshMenuItem.PerformClick();", it goes to

private void DataRefreshMenuItem_Click(object sender,
System.EventArgs e)
{
//blah, blah, blah
}

Obviously, this is because DataRefreshMenuItem.PerformClick is defined
as this.DataRefreshMenuItem_Click in the following code:

this.DataRefreshMenuItem.Click += new
System.EventHandler(this.DataRefreshMenuItem_Click );

Can anyone please tell me why call an intermediate method,
"DataRefreshMenuItem.Click" instead of calling
DataRefreshMenuItem_Click?

I could accomplish the same thing by defining a method,
DataRefreshMenuItem_Click, and call this exact method from
POToolBar_ButtonClick.

Thanks!

Mar 23 '07 #1
2 2068
Curious wrote:
private void POToolBar_ButtonClick (object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (e.Button.Tag.ToString())
{
case "Refresh":
DataRefreshMenuItem.PerformClick(); // This calls
PerformClick causes the item to act as if it had been clicked by the
user. If you didn't add the event handler (perhaps you're writing a
general purpose automation library, for example), then you might have
difficulty figuring out what is bound to the Click event.

Samples are usually didactic in nature: sometimes they have bugs, and
often don't represent best practice, etc.

-- Barry

--
http://barrkel.blogspot.com/
Mar 23 '07 #2
PS

"Curious" <fi********@yahoo.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi All,

I saw a sample code and I think it makes life more complicated with
delegate and event handler stuff. Please correct me if I were wrong.

I have a method defined as below:

private void POToolBar_ButtonClick (object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (e.Button.Tag.ToString())
{
case "Refresh":
DataRefreshMenuItem.PerformClick(); // This calls
the delegate
break;
case "Add":
NewOrderItemButton.PerformClick();
break;
case "Save":
SaveOrderButton.PerformClick();
break;
}
}

Then in the debugger, when executing
"DataRefreshMenuItem.PerformClick();", it goes to

private void DataRefreshMenuItem_Click(object sender,
System.EventArgs e)
{
//blah, blah, blah
}

Obviously, this is because DataRefreshMenuItem.PerformClick is defined
as this.DataRefreshMenuItem_Click in the following code:

this.DataRefreshMenuItem.Click += new
System.EventHandler(this.DataRefreshMenuItem_Click );

Can anyone please tell me why call an intermediate method,
"DataRefreshMenuItem.Click" instead of calling
DataRefreshMenuItem_Click?

I could accomplish the same thing by defining a method,
DataRefreshMenuItem_Click, and call this exact method from
POToolBar_ButtonClick.
This code probably started by someone creating an event handler for a menu
item click. Later a toolbar was also used to be able to perform the same
action. The code that was in the click event should have been refactored to
a method (Extract Method). Then the DataRefreshMenuItem_Click and the
"Refresh" case statement should have called this method. Using the
PerformClick was the quick and dirty way to get the same result.

PS
Mar 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: 0to60 | last post by:
More importantly, WHY do I wanna do this? Well, I have events that I raise asynchronously like this: foreach(eventhandler handler in eventname.getinvocationlist()) handler.begininvoke(); ...
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
4
by: Jon Davis | last post by:
If two delegates are created that point to the exact same method, and an event is assigned both delegates, ... myObj.MyEvent += new EventHandler(MyHandler); myObj.MyEvent += new...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
by: Kerry Jenkins | last post by:
I am having problems passing an Event Delegate as an argument to a method that accepts a delegate argument. I get the following error message: 'Public Event ProgressChanged(sender As Object, e...
5
by: Mats Larsson | last post by:
Hi, I have an example in C# for testing if a handler has been assigned to the delegate, I would like to do the same in VB.NET but I don't understand how. The C# code is copied from the Northwind...
5
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
7
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the...
3
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.