I read this article about imitating VB6's control arrays on C#. What it suggested is this: Assuming I am trying to employ a single Click event on multiple buttons - b1, b2, b3 - I would instantiate a Button class (bx) and create its eventhandler. Then, I add eventhandlers to these (b1, b...) pointing to bx's.
this.b1.Click += EventHandler(bx_Click);
this.b2.Click += EventHandler(bx_Click);
It worked, but I'm stopped on one question: How do I know who's the sender? I mean how do I use the sender par to identify who's b1 or b2 or b3?
Well, I know they have their Name property, but are there better (cleaner) approach on this?
[tt]
private void bx_Click(object sender, EventArgs e)
{
Button btnHndr;
btnHndr = (Button)sender;
switch(btnHndr.Name)
{
case "b1":
MessageBox.Show("Button 1"
break;
case "b2":
MessageBox.Show("Button 2"
break;
case "b3":
MessageBox.Show("Button 3"
break;
}
}[/tt]
TIA,
this.b1.Click += EventHandler(bx_Click);
this.b2.Click += EventHandler(bx_Click);
It worked, but I'm stopped on one question: How do I know who's the sender? I mean how do I use the sender par to identify who's b1 or b2 or b3?
Well, I know they have their Name property, but are there better (cleaner) approach on this?
[tt]
private void bx_Click(object sender, EventArgs e)
{
Button btnHndr;
btnHndr = (Button)sender;
switch(btnHndr.Name)
{
case "b1":
MessageBox.Show("Button 1"
break;
case "b2":
MessageBox.Show("Button 2"
break;
case "b3":
MessageBox.Show("Button 3"
break;
}
}[/tt]
TIA,