Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a win form with 43 textboxes and I am wanting to enable a button if any of the boxes has been changed. the following code is what I have. This is in the Form_Load routine. I added the message box to see if it is working but it is not. What am I missing or doing wrong.

What I have tried:

'Setup TextChanged flag for all textboxes
        For Each Control As Control In Me.Controls
            If Control.GetType = GetType(TextBox) Then
                Dim TextBox As TextBox = DirectCast(Control, TextBox)
                AddHandler TextBox.TextChanged, AddressOf TextBoxTextChanged_Handler
                MsgBox(TextBox.Name & " was changed.") 'Added this message box to see if the sub is working 
            End If
        Next


I have the following but it doesn't fire:

' TextChangedEventHandler delegate method.
    Sub TextBoxTextChanged_Handler(ByVal sender As Object, ByVal e As EventArgs)
        Dim TextBox As TextBox = DirectCast(sender, TextBox)
        MsgBox(TextBox.Name & " was changed.")
    End Sub
Posted
Updated 5-Feb-20 20:15pm
Comments
Ralf Meier 6-Feb-20 4:51am    
I'm not complete sure but I suppose that at the end of Form_Load the Controls of the Form are instances - not before. I would try it with another event from the Form.
Another point : For me it's allways weird to give a variable the same Name as the Type to which it's refering (For each control as Control ... - better : for each c as control)
Richard Deeming 6-Feb-20 9:25am    
For Windows Forms, the controls will exist as soon as the designer-generated InitializeComponent method returns. That's usually called from the constructor, so the controls will exist long before the Load event fires. :)

1 solution

It is possible that textboxes are not directly in form's Controls property; they could be contained by another control (GroupBox, etc...).

You could put a breakpoint on the foreach line and see which controls you grab.

Or you could statically define the handler in IDE by selecting all textboxes and affecting them the handler.
 
Share this answer
 
Comments
bgcwaterman 6-Feb-20 9:57am    
The textboxes are in group boxes. I will need to figure out how to do this for text boxes in group boxes.
phil.o 6-Feb-20 10:06am    
You could do
For Each ctl As Control In Me.Controls
   If ctl.GetType = GetType(GroupBox) Then
      For Each tb As Control in ctl.Controls
         '' ...
but imho the solution to statically assign the handler from IDE is much simpler (unless you are building the controls at runtime).
bgcwaterman 6-Feb-20 10:08am    
How would you do the IDE method? thanks
phil.o 6-Feb-20 10:12am    
- Go to the design view of the form.
- Select all text boxes.
- In the 'Properties' window (usually on the right), there is an 'Events' button (which allows to display events for selected control(s) instead of properties).
- Go to the TextChanged event, and select the handler.
And don't forget to delete the part where you tried to do it programmatically, which won't be needed anymore.
bgcwaterman 6-Feb-20 10:21am    
I don't have a "Events" button in the properties window after I select the textboxes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900