adapt control array to event

  • Thread starter Thread starter gcl
  • Start date Start date
G

gcl

Hi,

How to associate the dynamic controls array
with its event?

For example, creating a number of button, like 100, etc..

Button arrButton[100] .....

In VB6, when u copy and paste a button
it auto create the next array and
events such as ButtonClick(Index as Integet)

how to do that in C#?
 
In .NET, control arrays do not *necessarily* exist. In C#, if you want
multiple controls to be handled by the same event handler, then you need to
hookup the event handler manually...

public void ButtonHandler(object sender, EventArgs e)
{
//do something here...
}

and in your load event:

private void Page_Load(object sender, EventArgs e)
{
btn1.Click += new EventHandler(ButtonHandler);
btn2.Click += new EventHandler(ButtonHandler);
btn3.Click += new EventHandler(ButtonHandler);
}

or, if you have added the controls to an array, you can loop through the
array and set the handlers too.

HTH,

Bill P.
 
gcl,
To add to Bill Priess comments.

The sender parameter of the event handler then identifies which button was
clicked. You can cast it to Button to get the actual button object.

Hope this helps
Jay
 
I am not sure ..

What I do is checking the sender object's property
to identify which button sent

-----Original Message-----
gcl,
To add to Bill Priess comments.

The sender parameter of the event handler then identifies which button was
clicked. You can cast it to Button to get the actual button object.

Hope this helps
Jay

gcl said:
Hi,

How to associate the dynamic controls array
with its event?

For example, creating a number of button, like 100, etc..

Button arrButton[100] .....

In VB6, when u copy and paste a button
it auto create the next array and
events such as ButtonClick(Index as Integet)

how to do that in C#?


.
 
gcl,
You could cast sender to a Button variable, then check the Button.Name
property for which button you have. Assuming you set Button.Name to uniquely
identify the button. Actually you could use any property you want of Button.
Or you could derive a class from Button and add customer actions (functions)
& properties on it.

Something like:

private void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (button.Name = "Button1")
{
// do something with button1.
}
}

I would normally create a new class that inherits from Button and having the
click event operate polymorphically on this new class.

Something like:

private void ColorizingButton_Click(object sender, EventArgs e)
{
ColorizingButton button = (ColorizingButton)sender;
this.BackColor = button.Color;
}

class ColorizingButton : Button
{
private Color m_color;

ColorizingButton(Color color)
{
m_color = color;
}

public Color Color
{
get
{
return m_color;
}
}
}

Then I would have an array of ColorizingButtons, where each button had a
different Color for its color property. When that button was clicked, the
background color of the container would be changed to the color of that
button.

If you make the Color property above read/write, you can set the property in
the Properties window when you design the form...

Hope this helps
Jay

gcl said:
I am not sure ..

What I do is checking the sender object's property
to identify which button sent

-----Original Message-----
gcl,
To add to Bill Priess comments.

The sender parameter of the event handler then identifies which button was
clicked. You can cast it to Button to get the actual button object.

Hope this helps
Jay

gcl said:
Hi,

How to associate the dynamic controls array
with its event?

For example, creating a number of button, like 100, etc..

Button arrButton[100] .....

In VB6, when u copy and paste a button
it auto create the next array and
events such as ButtonClick(Index as Integet)

how to do that in C#?


.
 
Back
Top