Solved

Visual Basic 2008 change enabled state of all buttons on form

Posted on 2010-09-09
12
465 Views
Last Modified: 2012-08-13
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
Comment
Question by:CTEC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
12 Comments
 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33640800
add all button in groupbox or panel
0
 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33640820
then use groupbox.enable = false
or panel.enable = false
0
 
LVL 6

Assisted Solution

by:FactorB
FactorB earned 250 total points
ID: 33640838
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

Author Comment

by:CTEC
ID: 33640911
Thanks,

It syas:
Type 'CommandButton' is not defined
0
 
LVL 6

Expert Comment

by:FactorB
ID: 33640950
Change it simply to Button
0
 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33640966
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 33641015
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
 

Author Comment

by:CTEC
ID: 33641060
Thanks,

how would i call that?

SetButtons(Button, Enabled)  ?
0
 

Author Comment

by:CTEC
ID: 33641065
sorry just read it again and it says :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33641074
From within the Form, you would do:

   SetButtons(Me, True) ' Enable them all

Or:

   SetButtons(Me, False) ' Disable them all
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33641084
Good luck!....  =)
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question