PC Review


Reply
Thread Tools Rate Thread

what is correct way to assign a custom event handler?

 
 
Rich P
Guest
Posts: n/a
 
      30th Dec 2009
I have a (test) form with 3 panels and a button on each panel. I want
to assign a single event handler to the 3 buttons using an anonymous
method (just experimenting here) - this based on an example in a prior
post (of mine). The compiler is complaining as noted below. What is the
correct way to do this?

using...
...
namespace CsharpProj2008
{
public partial class Form1 : Form
{
string[] s1 = new string[] { "test1", "test2", "test3 };

public frmMultiBtns()
{
InitializeComponent();
}

//>>---complaining in this block

button1.Click += (sender, e) => GeneralPurposeClickHandler(sender, e,
s1[0]);
button2.Click += (sender, e) => GeneralPurposeClickHandler(sender, e,
s1[1]);
button3.Click += (sender, e) => GeneralPurposeClickHandler(sender, e,
s1[2]);

//>>

private void Form1_Load(object sender, EventArgs e)
{
//can't use "sender" here because already declared
}

void GeneralPurposeClickHandler(object sender, EventArgs e, string s2)
{
Control ctl = (Control)sender;
Panel panel = (Panel)ctl.Parent;
Console.WriteLine(ctl.Name + " " + panel.Name + " " + s2);

}

}
}


Thanks,

Rich

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      30th Dec 2009
"Rich P" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> //>>---complaining in this block
>
> button1.Click += (sender, e) => GeneralPurposeClickHandler(sender, e,
> s1[0]);
> button2.Click += (sender, e) => GeneralPurposeClickHandler(sender, e,
> s1[1]);
> button3.Click += (sender, e) => GeneralPurposeClickHandler(sender, e,
> s1[2]);
>
> //>>


From what you posted, this code is outside of any methods. Put it in the
constructor.


 
Reply With Quote
 
Rich P
Guest
Posts: n/a
 
      30th Dec 2009
doh! thanks. Works as advertised now.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with custom event handler =?Utf-8?B?dnZlbms=?= Microsoft VB .NET 3 12th Oct 2005 07:47 PM
Custom Event Handler SDF Microsoft ASP .NET 1 5th Jan 2005 04:01 PM
Custom Event Handler Aaron Microsoft C# .NET 3 11th Aug 2004 12:37 AM
Assign session onEnd event handler at run-time Pavils Jurjans Microsoft ASP .NET 3 21st Apr 2004 05:44 PM
custom event handler Philip Townsend Microsoft ASP .NET 1 16th Oct 2003 09:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:30 PM.