tags:

views:

1114

answers:

4

VB 2008.

I have several text boxes on a form and I want each of them to use the same event handler. I know how to manually wire each one up to the handler, but I'm looking for a more generic way so if I add more text boxes they will automatically be hooked up to the event handler.

Ideas?

EDIT: Using the C# sample from MusiGenesis (and with the help of the comment left by nick), I wrote this VB code:

Private Sub AssociateTextboxEventHandler()
  For Each c As Control In Me.Controls
    If TypeOf c Is TextBox Then
      AddHandler c.TextChanged, AddressOf tb_TextChanged
    End If
  Next
End Sub

Thanks a lot! SO is great.

A: 

It might be possible with a macro, but otherwise, I a m not aware of anything that would automatically wire an control to a generic handler.

The only easy way I know of would be to select all appliciable textboxes, and simply set the eventhandler for the click event at the same time, but that isn't automatic.

Mitchel Sellers
+1  A: 

You could recursively iterate over the Controls collection in the OnLoad of the form and assign the event handler to any text boxes you find.

Jeff Yates
I would love to see the code to do this in VB.NET. This kind of thing is very easy in Delphi, but I've not seen a clear way to do it in VB.NET yet.
JosephStyons
For Each c as control in me.controlsif typeof c is textbox c.addhandler c.textchanged, addressof textboxhandlerend ifnext
tom.dietrich
+6  A: 

Do something like this in your form's load event (C#, sorry, but it's easy to translate):

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control ctrl in this.Controls)
    {
        if (ctrl is TextBox)
        {
            TextBox tb = (TextBox)ctrl;
            tb.TextChanged += new EventHandler(tb_TextChanged);
        }
    }

}

void tb_TextChanged(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    tb.Tag = "CHANGED"; // or whatever
}

It's not properly recursive (it won't find text boxes on panels, for example), but you get the idea.

MusiGenesis
In VB.NET, the equivolent to += on an event is to use AddHandler
Nick
A: 

I think this will update all text controls.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            UpdateControls(Me)
    End Sub


    Private Sub UpdateControls(ByVal myControlIN As Control)
        Dim myControl

        For Each myControl In myControlIN.Controls
            UpdateControls(myControl)
            Dim myTextbox As TextBox
            If TypeOf myControl Is TextBox Then
                myTextbox = myControl
                AddHandler myTextbox.TextChanged, AddressOf TextBox_TextChanged
            End If
        Next

    End Sub


    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox("text changed in " & CType(sender, TextBox).Name)
    End Sub
osp70
You could say "If myControl.HasChildren Then UpdateControls(myControl)".
MusiGenesis