Brodequin Posted November 16, 2003 Posted November 16, 2003 Hi all. I'm having difficulty trying to figure out how to tab to the next textbox in a list of textboxes. In other words, if the maxlength of my textbox is five and you type "12345", tab or set focus on the next textbox. I know this works: private void txtAmount1_TextChanged(object sender, System.EventArgs e) { if ( txtAmount1.TextLength == txtAmount1.MaxLength) txtAmount2.Focus(); } Now, what do I do if I have 25 textboxes on a form named txtAmount1...txtAmount25? I don't want to have 25 TextChanged events. In VB6, I would use a control array for txtAmount to iterate through the textboxes. From the examples I've seen using "equivalent" control arrays in C#, the only way I can iterate through a list of controls is from a triggered event (like a button click). Hopefully I'm making myself clear. Any suggestions? Quote
*Experts* Volte Posted November 16, 2003 *Experts* Posted November 16, 2003 Try this:Me.SelectNextControl(Me.ActiveControl, True, True, False, False) Quote
Brodequin Posted November 16, 2003 Author Posted November 16, 2003 Try this:Me.SelectNextControl(Me.ActiveControl, True, True, False, False) Thanks for your reply. I see how this works, but I don't think this will solve my problem. With this I would still need 25 TextChanged events, correct? Do anyone have any other suggestions (and one that is in C#)? Quote
pendragon Posted November 16, 2003 Posted November 16, 2003 This is what I have used. private void textBox1_TextChanged(object sender, System.EventArgs e) { TextBox t; t = (TextBox)this.ActiveControl; if (t.TextLength == t.MaxLength) { this.SelectNextControl(this.ActiveControl, true, true, false, false); } } You can then set the TextChanged event for each of your text box's to textbox1_TextChanged Quote
Brodequin Posted November 16, 2003 Author Posted November 16, 2003 pendragon, Thanks. Your code works! The only thing is that I'm disappointed that I will still have 25 TextChanged events, but at least I only have to change the code in one location. Beggars can't be choosers, eh? private void txtAcct1_TextChanged(object sender, System.EventArgs e) { TextBox t; t = (TextBox)this.ActiveControl; if (t.TextLength == t.MaxLength) { this.SelectNextControl(this.ActiveControl, true, true, false, false); } } private void txtAcct2_TextChanged(object sender, System.EventArgs e) { txtAcct1_TextChanged(sender,e); } . . . private void txtAcct25_TextChanged(object sender, System.EventArgs e) { txtAcct1_TextChanged(sender,e); } Quote
*Experts* mutant Posted November 16, 2003 *Experts* Posted November 16, 2003 You dont need to have as many event handlers, in your designer generated code find where the event handlers get assigned and make it point to the same method for all textboxes this.TextBox2.TextChanged += new System.EventHandler(this.TextBox1_TextChanged); Or if you want, go to the properties, click on the lightning bolt icon and then from the list select text changed event and then from dropdown list select the method you want to handle the event. Quote
Brodequin Posted November 16, 2003 Author Posted November 16, 2003 You dont need to have as many event handlers, in your designer generated code find where the event handlers get assigned and make it point to the same method for all textboxes this.TextBox2.TextChanged += new System.EventHandler(this.TextBox1_TextChanged); Or if you want, go to the properties, click on the lightning bolt icon and then from the list select text changed event and then from dropdown list select the method you want to handle the event. YES, YES, YES!!! This is exactly what I'm looking for. Problem solved, thanks very much. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.