Re: create an event handler for control array in C#

From: Joakim Karlsson (jkarlsson_at_NOSPAMjkarlsson.com)
Date: 01/10/05


Date: Mon, 10 Jan 2005 15:41:31 +0100

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


Loading