Can I modify the existing textbox class?

  • Thread starter Thread starter cgchris99
  • Start date Start date
C

cgchris99

Guest
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
 
Excuse my ignorance ,
but if it is a proprty you can just loop through the form's controls collection .
Visual Basic:
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

Visual Basic:
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?!
 
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.

Visual Basic:
    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.
 
This looping routine to add to every textbox looks real interesting. That just might work perfectly for me.

Thanks
 
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.
 
Back
Top