User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2008
Posts: 7
Reputation: Ken-Barrie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Ken-Barrie Ken-Barrie is offline Offline
Newbie Poster

Handler for an array of controls created in code

  #1  
Jun 21st, 2008
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 454
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 78
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro in Training

Re: Handler for an array of controls created in code

  #2  
Jun 21st, 2008
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
  1. TextBox[] textArray;
  2. public Form1()
  3. {
  4. InitializeComponent();
  5. textArray = new TextBox[10];
  6. for (int i = 0; i < 10; i++)
  7. {
  8. TextBox textBox = new TextBox();
  9.  
  10. textBox.Name = "Name" + i ;
  11. textBox.Location = new Point(0, (i + 1) * textBox.Height);
  12. textArray[i] = textBox;
  13. textArray[i].Click += new EventHandler(ArrayOnClick);
  14. this.Controls.Add(textBox);
  15. }
  16. }

  1. private void ArrayOnClick(object sender, EventArgs e)
  2. {
  3. TextBox textBox =(TextBox) sender;
  4. MessageBox.Show(textBox.Name );
  5. switch ( textBox.Name ){
  6. case "Name0":
  7. //Code for Name0 TextBox
  8.  
  9. break;
  10. case "Name1":
  11. //Code for Name1 TextBox
  12. break;
  13.  
  14. }
  15. }
KSG
Reply With Quote  
Join Date: Jun 2008
Posts: 7
Reputation: Ken-Barrie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Ken-Barrie Ken-Barrie is offline Offline
Newbie Poster

Re: Handler for an array of controls created in code

  #3  
Jun 22nd, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 4:11 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC