Solved

Visual Basic 2008 change enabled state of all buttons on form

Posted on 2010-09-09
12
462 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 85

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 85

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 85

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now