PC Review


Reply
Thread Tools Rate Thread

create an event handler for control array in C#

 
 
=?Utf-8?B?ZnJhbmtjdmM=?=
Guest
Posts: n/a
 
      10th Jan 2005
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
 
Reply With Quote
 
 
 
 
Joakim Karlsson
Guest
Posts: n/a
 
      10th Jan 2005
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");
> }
>

 
Reply With Quote
 
Joakim Karlsson
Guest
Posts: n/a
 
      10th Jan 2005
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");
>> }
>>

 
Reply With Quote
 
Benoit Vreuninckx
Guest
Posts: n/a
 
      10th Jan 2005
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
 
Reply With Quote
 
Michael Groeger
Guest
Posts: n/a
 
      10th Jan 2005
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" <(E-Mail Removed)> schrieb im Newsbeitrag
news:7011A430-A05E-40B0-945E-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      10th Jan 2005
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


 
Reply With Quote
 
=?Utf-8?B?ZnJhbmtjdmM=?=
Guest
Posts: n/a
 
      10th Jan 2005
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
>
>
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      10th Jan 2005
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


 
Reply With Quote
 
Frank Kwong
Guest
Posts: n/a
 
      29th Jan 2005
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How Do You Add Event Handler to 50 button array???? Armin Zingler Microsoft VB .NET 0 13th May 2008 11:36 PM
Add Event Handler to array of controls? =?Utf-8?B?UmljaA==?= Microsoft VB .NET 4 15th Sep 2005 09:51 PM
Create new event handler or override existing handler? =?Utf-8?B?V29vbiBLaWF0?= Microsoft Dot NET Framework Forms 1 5th Jan 2004 08:08 AM
Dynamic control array & Command Event Handler Sriram Microsoft ASP .NET 0 4th Dec 2003 08:47 PM
How to create Event Handler for Dynamically added control Amit D.Shinde Microsoft VB .NET 15 25th Sep 2003 10:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:14 PM.