•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 423,497 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,678 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 461 | Replies: 2 | Solved
![]() |
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
I create an array of 16 text boxes, etc in code. I need to detect which one is clicked. I have created an event handler for each that points to the same handler routine for all 16
for(int box = 0; box < 16; box ++)
{
txtArray[box].Click += new System.EventHandler(txtArray_n_Click);
}
There I get stuck trying to identify which one called the handler. Been reading up on Delegates, but I'm not sure which direction to travel from here. I could spend a lot of time on that path to no profit other than my own education!
Any pointers?
for(int box = 0; box < 16; box ++)
{
txtArray[box].Click += new System.EventHandler(txtArray_n_Click);
}
There I get stuck trying to identify which one called the handler. Been reading up on Delegates, but I'm not sure which direction to travel from here. I could spend a lot of time on that path to no profit other than my own education!
Any pointers?
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 454
Reputation:
Rep Power: 1
Solved Threads: 78
Hi, When create the textbox give unique name. And in Click event you receive the sender object, Typecast it to textbox and check the name you want.
Ex
Ex
C# Syntax (Toggle Plain Text)
TextBox[] textArray; public Form1() { InitializeComponent(); textArray = new TextBox[10]; for (int i = 0; i < 10; i++) { TextBox textBox = new TextBox(); textBox.Name = "Name" + i ; textBox.Location = new Point(0, (i + 1) * textBox.Height); textArray[i] = textBox; textArray[i].Click += new EventHandler(ArrayOnClick); this.Controls.Add(textBox); } }
C# Syntax (Toggle Plain Text)
private void ArrayOnClick(object sender, EventArgs e) { TextBox textBox =(TextBox) sender; MessageBox.Show(textBox.Name ); switch ( textBox.Name ){ case "Name0": //Code for Name0 TextBox break; case "Name1": //Code for Name1 TextBox break; } }
KSG
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Yes. That will do nicely.
Your
textBox.Name = "Name" + i ;
becomes, for me
txtArray[i].Name = i.ToString();
[Shame on me. Why did I not think of giving them a name!]
Then I pick up your line in the handler
TextBox textBox =(TextBox) sender;
And I’m away, with a reference to which one is knocking at the door.
I was frustrated that the event handler did not pass me enough info in the Sender object or EventArgs to determine which control I was dealing with. I could determine the text in the box but not the name. IT HAD NO NAME.
Your
textBox.Name = "Name" + i ;
becomes, for me
txtArray[i].Name = i.ToString();
[Shame on me. Why did I not think of giving them a name!]
Then I pick up your line in the handler
TextBox textBox =(TextBox) sender;
And I’m away, with a reference to which one is knocking at the door.
I was frustrated that the event handler did not pass me enough info in the Sender object or EventArgs to determine which control I was dealing with. I could determine the text in the box but not the name. IT HAD NO NAME.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
Array access activation api blogger blogging blogs code code injection combo dani daniweb data debugging development dreamweaver dropdownlist epilepsy gdata google gpl griefers hackers html innovation javascript key linux microsoft module net news openbsd product programming reuse rss serial source tags vista web wysiwyg xml
- Need help with this game i've created (VB.NET)
- create textbox in code and set it equal to one on form. (C#)
Other Threads in the C# Forum
- Previous Thread: how to check items of checked list box ?
- Next Thread: multiline textbox to .doc


Linear Mode