create an event handler for control array in C#

G

Guest

I created a control array at runtime using the following code. I also wanted
to code the controlarray click event. My code can respond to click event
but cannot identify the specific control clicked on, which is important to my
application.

Appreciate your help.

Frank
---------------------------
private void initBoard()
{
this.Width=250;
this.Height=250;
buttonArray = new Button[4,4];
for (int i = 1; i<=3; i++)
for (int j=1; j<=3;j++)
{
Button aButton = new Button();
aButton.Click += new System.EventHandler(ClickHandler);
aButton.Width=50;
aButton.Height=50;
aButton.Top = i * 50;
aButton.Left = j*50;
buttonArray[i,j]=aButton;
// Add the button to the controls collection of the form
this.Controls.Add(aButton);
}
}

private void ClickHandler(object sender, System.EventArgs e)
{
MessageBox.Show("Button" +((Button) sender).Tag.ToString() + " was
clicked");
}
 
J

Joakim Karlsson

The control that is clicked will be passed as the sender argument of
your ClickHandler method.

/Joakim
 
J

Joakim Karlsson

Oops! I jumped the gun there a little :)

You've already realized that I see.

You never set the Tag property of the buttons in the creation code, so
there is nothing for you to read there when you reach the event handler.

/Joakim

Joakim said:
The control that is clicked will be passed as the sender argument of
your ClickHandler method.

/Joakim
I created a control array at runtime using the following code. I also
wanted to code the controlarray click event. My code can respond to
click event but cannot identify the specific control clicked on, which
is important to my application.
Appreciate your help.

Frank
---------------------------
private void initBoard()
{
this.Width=250;
this.Height=250;
buttonArray = new Button[4,4];
for (int i = 1; i<=3; i++)
for (int j=1; j<=3;j++)
{
Button aButton = new Button();
aButton.Click += new
System.EventHandler(ClickHandler);
aButton.Width=50;
aButton.Height=50;
aButton.Top = i * 50;
aButton.Left = j*50;
buttonArray[i,j]=aButton;
// Add the button to the controls collection of
the form this.Controls.Add(aButton);
}
}

private void ClickHandler(object sender, System.EventArgs e)
{
MessageBox.Show("Button" +((Button) sender).Tag.ToString()
+ " was clicked");
}
 
B

Benoit Vreuninckx

frankcvc said:
I created a control array at runtime using the following code. I also wanted
to code the controlarray click event. My code can respond to click event
but cannot identify the specific control clicked on, which is important to my
application.

Appreciate your help.

Frank
---------------------------
private void initBoard()
{
this.Width=250;
this.Height=250;
buttonArray = new Button[4,4];
for (int i = 1; i<=3; i++)
for (int j=1; j<=3;j++)
{
Button aButton = new Button();
aButton.Click += new System.EventHandler(ClickHandler);
aButton.Width=50;
aButton.Height=50;
aButton.Top = i * 50;
aButton.Left = j*50;
buttonArray[i,j]=aButton;
// Add the button to the controls collection of the form
this.Controls.Add(aButton);
}
}

private void ClickHandler(object sender, System.EventArgs e)
{
MessageBox.Show("Button" +((Button) sender).Tag.ToString() + " was
clicked");
}

Apart from the button identification problem, are you correctly
iterating over the array of buttons ? The first row and column aren't
populated with buttons, but that might be your intention.

Regards,
Benoit
 
M

Michael Groeger

i would appreciate something like this:

class MyButton : Button
{
int _i,_j;

public int _I
{
get { return _i; }
}

public int _J
{
get { return _j; }
}

public MyButton(int i, int j)
{
_i = i;
_j = j;
}
}

In your code replace:
Button aButton = new Button();

with:
MyButton aButton = new MyButton(i,j);

And write a appropriate handler:
private void ClickHandler(object sender, System.EventArgs e)
{
MyButton button = (MyButton)sender;
MessageBox.Show(string.Format("Button ({0}.{1}) was clicked", button._I,
button._J));
}

Michael
 
C

Cor Ligthert

Frank,

In my opinion do you do nothing with your button array.
You have it already added to the collection "this.controls", so there it is
and as long as your form object is there it will be there until you remove
it.

When you give dynamicly your button a name from 0 to 15 or as others said
use the tage for that, than
you can get that in the event using the code you almost used however than.

messagebox.show((Button)sender).Name.ToString() + ect)

Just my idea

Cor
 
G

Guest

Many thanks for all of the responses to my question. Yes, all of these
suggestions are very relevant . The problem is solved by adding a tag to the
button.

However, in my click event, I can only identify the individual button but
not as a group member or its subscript. Of course, with proper taging I can
extract the subscript out from the tag. I am wondering if there is a more
straightforward way to do it like in the older VB.

Thanks again.

Frank
 
C

Cor Ligthert

Frank,
Many thanks for all of the responses to my question. Yes, all of these
suggestions are very relevant . The problem is solved by adding a tag to
the
button.

However, in my click event, I can only identify the individual button but
not as a group member or its subscript. Of course, with proper taging I
can
extract the subscript out from the tag. I am wondering if there is a more
straightforward way to do it like in the older VB.
Real because it makes me curious, why you want to do that? This is very
straigthforward in my idea.
(I keep saying you do not need the array).

When you want it using the designer, than you can create an array by using
the buttons which are created.

\\\
private void FormLoad(object sender, System.EventArgs e)
{Button[] but = {button1,button2};
foreach (Button bt in but)
{bt.Click += new System.EventHandler(this.button_Click);}
}
///

The rest is almost the same.

I don't know it this is the idea

Cor

Cor
 
F

Frank Kwong

In my case, I have a data collection control that when data comes it, it
fires up The DataIn event handler. I have 100 of these controls fired up
and the incoming data for each is saved into a buffer array. The I use a
timer to go in and scan the buffer and process the message accordingly.
If I can have an event array then I can process the data in the indexed
handler instead of saving it and process later. I cannot process the
data in the single DataIn handler as it would miss data coming in.

Thanks,

FK
 

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