Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Visual Basic 2008 change enabled state of all buttons on form

Hi,

I have an application in Visual Basic 2008 where there is a form with lots of buttons on.
Is there a way to control the Enabled state of all the buttons on the form?
I.e. Form1.AllButtons.Enabled = True?


Thanks

Daniel.,
0
CTEC
Asked:
CTEC
  • 3
  • 3
  • 3
  • +1
2 Solutions
 
ghayasurrehmanCommented:
add all button in groupbox or panel
0
 
ghayasurrehmanCommented:
then use groupbox.enable = false
or panel.enable = false
0
 
FactorBCommented:
Simple disable or enable command buttons or buttons ... change commandbutton to button..

Private Sub Disable_Controls()
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is CommandButton then ctrl.Enabled = False
Next
End Sub

Private Sub Enable_Controls()
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is CommandButton then ctrl.Enabled = True
Next
End Sub

I didn't had a time to check it...

Regards,
B.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CTECAuthor Commented:
Thanks,

It syas:
Type 'CommandButton' is not defined
0
 
FactorBCommented:
Change it simply to Button
0
 
ghayasurrehmanCommented:
use panel or groupbox instead of parsing the controls, that will be more efficient

or use


Private Sub Disable_Controls()
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is Button then ctrl.Enabled = False
Next
End Sub

Private Sub Enable_Controls()
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is Button then ctrl.Enabled = True
Next
End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That won't catch the Buttons if they are INSIDE a different container (than the form itself) or if they are in multiple containers...

...what is needed is a RECURSIVE method:

    SetButtons(Me, False)
    ' < or >
    SetButtons(Me, True)

Using this code:
    Private Sub SetButtons(ByVal cont As Control, ByVal EnabledState As Boolean)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is Button Then
                ctl.Enabled = EnabledState
            ElseIf ctl.HasChildren Then
                SetButtons(ctl, EnabledState)
            End If
        Next
    End Sub

Open in new window

0
 
CTECAuthor Commented:
Thanks,

how would i call that?

SetButtons(Button, Enabled)  ?
0
 
CTECAuthor Commented:
sorry just read it again and it says :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
From within the Form, you would do:

   SetButtons(Me, True) ' Enable them all

Or:

   SetButtons(Me, False) ' Disable them all
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Good luck!....  =)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now