Delegate and Event Handler

C

Curious

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!
 
B

Barry Kelly

Curious said:
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
 
P

PS

Curious said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top