Solved

Visual Basic 2008 change enabled state of all buttons on form

Posted on 2010-09-09
12
461 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

19 Experts available now in Live!

Get 1:1 Help Now