create an event handler for control array in C# | | |
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 | | | | re: create an event handler for control array in C#
The control that is clicked will be passed as the sender argument of
your ClickHandler method.
/Joakim
frankcvc wrote:[color=blue]
> 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");
> }
>[/color] | | | | re: create an event handler for control array in C#
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:[color=blue]
> The control that is clicked will be passed as the sender argument of
> your ClickHandler method.
>
> /Joakim
>
> frankcvc wrote:
>[color=green]
>> 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");
>> }
>>[/color][/color] | | | | re: create an event handler for control array in C#
frankcvc wrote:[color=blue]
> 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");
> }
>[/color]
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 | | | | re: create an event handler for control array in C#
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" <frankcvc@discussions.microsoft.com> schrieb im Newsbeitrag
news:7011A430-A05E-40B0-945E-02DCBC444273@microsoft.com...[color=blue]
> I created a control array at runtime using the following code. I also[/color]
wanted[color=blue]
> 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[/color]
my[color=blue]
> 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[/color] | | | | re: create an event handler for control array in C#
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 | | | | re: create an event handler for control array in C#
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:
[color=blue]
> 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
>
>
>[/color] | | | | re: create an event handler for control array in C#
Frank,
[color=blue]
> 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.
>[/color]
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 | | | | re: create an event handler for control array in C#
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! |  | Similar C# / C Sharp bytes | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|