473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

adapt control array to event

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#?
Nov 13 '05 #1
3 9905
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" <ln*******@comcast.net> wrote in message
news:0e****************************@phx.gbl...
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#?

Nov 13 '05 #2
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" <ln*******@comcast.net> wrote in message
news:0e****************************@phx.gbl...
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#?

Nov 13 '05 #3
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" <ln*******@comcast.net> wrote in message
news:03****************************@phx.gbl...
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" <ln*******@comcast.net> wrote in message
news:0e****************************@phx.gbl...
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#?

.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: James McGivney | last post by:
I have a project in ASP.NET using C#. I have a large number of Button Controls and a large number of ImageButton Controls. It would be convienent if I could refer to a specific control by it's...
13
by: Bernie | last post by:
Sorry, but this ia another whine about VB.Net's lack of Control Arrays. I am new to VB.Net and I'm building an application that uses variable number of Label controls that are created at run...
20
by: samean | last post by:
Hello, Could you explain me,In VB6 using control array,and how about VB.net. Thanks
2
by: Allan Bredahl | last post by:
Hi all Im currently constructing a Windows control that inherits from RichTextBox, but I'm having a bit of trouble with updating the control at design time. The senario is SIMPLIFIED as...
0
by: Allan Bredahl | last post by:
Hi all Im currently constructing a Windows control that inherits from RichTextBox, but I'm having a bit of trouble with updating the control at design time. The senario is SIMPLIFIED as...
0
by: Allan Bredahl | last post by:
Hi all Im currently constructing a Windows control that inherits from RichTextBox, but I'm having a bit of trouble with updating the control at design time. The senario is SIMPLIFIED as...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
4
by: Rich | last post by:
Hello, I have 3 textboxes and 1 combobox on a form. On entering the control I want to select all the text. I can make an array of textboxes like this: Dim arrTxt As TextBox() = {txt1, txt2,...
6
by: Rich | last post by:
Hello, I have an application that contains several checkboxes. I originally created this app in VB.Net 2003 and upgraded the app to VB.Net 2005. I understand the vb2005 supports control...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.