Guest cgchris99 Posted October 17, 2002 Posted October 17, 2002 I have about 100 textboxes on a form and they are in various tabs. I have had to add the following handlers to perform some code that I want on these events. Is there a way to modify the existing Textbox class to include these changes so I don't have to add this code 100 times for each of the textboxes on the form? You will notice the Procedure for TextBoxChanged is the same for every textbox on the form and so is TextBoxValidated, TextboxMouseDown, etc. Thanks for any help AddHandler txtTextBox1.TextChanged, AddressOf TextBoxChanged AddHandler txtTextBox1.Validated, AddressOf TextBoxValidated AddHandler txtTextBox1.MouseDown, AddressOf TextBoxMouseDown AddHandler txtTextBox1.Leave, AddressOf TextBoxLeave AddHandler txtTextBox1.KeyPress, AddressOf TextBoxKeypress AddHandler txtTextBox2.TextChanged, AddressOf TextBoxChanged AddHandler txtTextBox2.Validated, AddressOf TextBoxValidated AddHandler txtTextBox2.MouseDown, AddressOf TextBoxMouseDown AddHandler txtTextBox2.Leave, AddressOf TextBoxLeave AddHandler txtTextBox2.KeyPress, AddressOf TextBoxKeypress Quote
*Gurus* Derek Stone Posted October 17, 2002 *Gurus* Posted October 17, 2002 Private Sub TextBoxChanged(..., ...) Handles txtTextBox1.TextChanged, txtTextBox2.TextChanged, txtTextBox3.TextChanged, ... 'Code End Sub Quote Posting Guidelines
duncallen Posted October 17, 2002 Posted October 17, 2002 Excuse my ignorance , but if it is a proprty you can just loop through the form's controls collection . Dim ctr As Control For Each ctr In Me.Controls If TypeOf ctr Is Label Then ctr.Text = "Cool" End If Next or now you can make a method in Net handle multiple events Private Sub subforAllText(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress Dim txt As TextBox txt = sender End Sub I apologize if I completely missed the point of your post, otherwise i hope this helps?! Quote
*Experts* Bucky Posted October 17, 2002 *Experts* Posted October 17, 2002 Your second suggestion is what Derek said, duncallen. Your first idea can be modified, however, to add event handlers for all the textboxes, because the other way would make for a realllllllly long sub declaration. Dim ctl As Control Dim txt As TextBox For Each ctl In Me.Controls If TypeOf ctl Is TextBox Then txt = ctl AddHandler txt.TextChanged, AddressOf TextBoxChanged AddHandler txt.Validated, AddressOf TextBoxValidated AddHandler txt.MouseDown, AddressOf TextBoxMouseDown AddHandler txt.Leave, AddressOf TextBoxLeave AddHandler txt.KeyPress, AddressOf TextBoxKeypress ' etc etc End If Next You will also have to add a For-Each loop for looping through each TabPage, and then loop through its Controls collection. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Guest cgchris99 Posted October 17, 2002 Posted October 17, 2002 This looping routine to add to every textbox looks real interesting. That just might work perfectly for me. Thanks Quote
Guest lepernet Posted October 18, 2002 Posted October 18, 2002 You can add a "user control" to your project. This will appear like a borderless form in the designer. Drop a text box onto it and resize as appropriate. You can add whatever logic you want to the text box. When compiled the new user control will appear in the toolbox and you will be able to drop it onto the form. You can do the same with groups of controls. Quote
*Gurus* divil Posted October 18, 2002 *Gurus* Posted October 18, 2002 Or you could just inherit from the textbox to add whatever behaviour you need to it. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
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.