By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

create an event handler for control array in C#

P: n/a
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");
}

--
Frank
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
The control that is clicked will be passed as the sender argument of
your ClickHandler method.

/Joakim

frankcvc wrote:
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");
}

Nov 16 '05 #2

P: n/a
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 Karlsson wrote:
The control that is clicked will be passed as the sender argument of
your ClickHandler method.

/Joakim

frankcvc wrote:
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");
}

Nov 16 '05 #3

P: n/a
frankcvc wrote:
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
Nov 16 '05 #4

P: n/a
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

"frankcvc" <fr******@discussions.microsoft.com> schrieb im Newsbeitrag
news:70**********************************@microsof t.com...
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");
}

--
Frank

Nov 16 '05 #5

P: n/a
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
Nov 16 '05 #6

P: n/a
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

"Cor Ligthert" wrote:
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

Nov 16 '05 #7

P: n/a
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
Nov 16 '05 #8

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.