1

I have a simple vb 2010 form that I want to be able to set the enabled stated of all buttons on. I have made similar functions for checkboxes and numericalUpDown controls that function as required, but for buttons the function does not seem to operate correctly.

Private Sub setButtonsState(ByVal state As Boolean)
    Dim cControl As Control
    For Each cControl In Me.Controls
        If (TypeOf cControl Is Button) Then
            cControl.Enabled = state
        End If
    Next cControl
End Sub

Which is called like so: setButtonsState(True) This doesnt seem to have any effect at all with buttons, though buttonName.Enabled = True works OK... Im pretty much what you'd call a noob at VB, so anyone help me out? Ta

3 Answers 3

4

Unless all of your buttons belong directly to the Form this is run in, then they won't be accessed because this doesn't cruise down through the control tree to get to them. A slightly different approach might be a more recursive method:

Private Sub setButtonsState(ByVal controls as System.Windows.Forms.Control.ControlCollection, ByVal state as Boolean)

    For Each ctrl as Control in controls
        If (ctrl.HasChildren) Then setButtonsState(ctrl.Controls, state)

        If (TypeOf ctrl Is Button) Then ctrl.Enabled = state
    Next

End Sub
7
  • That might be the case as the buttons are in a group box. However, an error is given "'HasControls' is not a member of 'System.Windows.Forms.Control'."...
    – Toby
    Oct 11, 2012 at 13:39
  • 1
    I think he might mean .haschildren For Example... If (ctrl.HasChildren) then...*continue.
    – Ccorock
    Oct 11, 2012 at 13:43
  • tried changing to hasChildren, but then an excpetion for unhandled invalidCast occurs for setButtonsState(ctrl.Controls, state)
    – Toby
    Oct 11, 2012 at 13:44
  • 1
    I had to change the setButtonState sub arguments to (ByVal controls as windows.forms.control.controlcollection, ByVal state as boolean). You might need this more specific reference to "control".
    – Ccorock
    Oct 11, 2012 at 13:52
  • 1
    @Ccorock: Yes running a sample in my own IDE I just discovered the disparity in the IDE's assumption. Oct 11, 2012 at 13:55
1

It works on my form. Are you sure you are setting state to a true value? you many want to dim a variable called "state" inside the subroutine just to test it. Set state to true and disable all of your buttons.

Try to retrieve the count of controls returned. To make sure you are correctly referencing the controls.

Demonstrating set boolean to True for testing:

*Dim state As Boolean = True*

    Dim cControl As Control
    For Each cControl In Me.Controls
        If (TypeOf cControl Is Button) Then
            cControl.Enabled = state
        End If
    Next cControl

Check the count of controls:

 For Each cControl In (Your Form Name Ex. FrmMain1).Controls
                    If (TypeOf cControl Is Button) Then
                        cControl.Enabled = state
                        *debug.writeline(cControl.Name)*
                    End If
                Next cControl
3
  • using the form name gives an error: "'Test1.FormName' cannot refer to itself through its default instance; use 'me' instead"
    – Toby
    Oct 11, 2012 at 13:32
  • setting state = true inside the sub doesn't work either so must be the actual logic itself, which is strange considering it worked for every other control type I've tried so far... thanks for your help though.
    – Toby
    Oct 11, 2012 at 13:37
  • 1
    Scratch that then. Try instead debug.writeline(Button1.Parent.Name.Tostring) this should justify whether the form is in fact the parent of that control, otherwise it will give you the parent of the control.
    – Ccorock
    Oct 11, 2012 at 13:38
0

I would suggest you avoid recursion, if possible. Below approach has a much better debug-ability.

First, Linearise ControlCollection tree into array of Control - generic extension method, no recursion, LINQ or GetNextControl.

Then you can write like this:

Private Sub setButtonsState(ByVal controls As ControlCollection, ByVal state As Boolean)
  For Each ctrl As Control In controls.GetAllControlsOfType(Of Button)()
    ctrl.Enabled = state
  Next
End Sub
3
  • As a noob two Q's A) why avoid recursion B) In your eg isn't the recursion just moved from the state change sub to the getAllControlsOfType sub? Ta
    – Toby
    Oct 11, 2012 at 14:04
  • 1
    @Toby: A) because you cannot easily debug it, suppose you have 100 buttons nested in 9 levels. With recursion, you don't even know if they were all picked up by your code. With the above approach, you can see them all in watch screen, see button count etc. B) No, getAllControlsOfType does not call itself. Oct 11, 2012 at 14:08
  • 1
    @Toby: another concern would be that on more complicated tasks you might get a StackOverflow exception because your recursion never finished. Then you don't even know where it died. Take a look at this article, the bad starts at page 2. Oct 11, 2012 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.