Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ToggleButton Control 1

Status
Not open for further replies.

tgmoon

Technical User
Jan 31, 2001
54
GB
Hi. I'm creating a form with 10 toggle buttons on it forming a grid, and i'm trying to set it up so that if one is depressed, all the others are raised, and so on with each button.

Assuming there were only 2 buttons, i'd use the code below

Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
Togglebutton2.Value = False
End If
End Sub

Private Sub ToggleButton2_Click()
If ToggleButton2.Value = True Then
Togglebutton1.Value = False
End If
End Sub

Obviously, with 10 Toggles, i'd add ToggleButton3 etc, the code starts to get a little unwieldly!

Can anyone help with suggestions on how to shorten it down, assuming it's possible?

Many thanks

Thom
 
Be careful with the Click event - in case of ToggleButton it is also called when you change the value programmatically. Without creating a class to handle an array of controls (see you can (destroy collection objects when closing userform):

Code:
Dim colToggles As Collection
Dim bExecuteEvent As Boolean

Private Sub UserForm_Initialize()
Set colToggles = New Collection
For Each cControl In Me.Controls
    If TypeName(cControl) = "ToggleButton" Then colToggles.Add cControl
Next cControl
bExecuteEvent = True
End Sub

Private Sub ToggleButton1_Change()
If bExecuteEvent Then Call SetToggleValues(Me.Controls("ToggleButton1"))
End Sub

Private Sub ToggleButton2_Change()
If bExecuteEvent Then Call SetToggleValues(Me.Controls("ToggleButton2"))
End Sub

Private Sub ToggleButton3_Change()
If bExecuteEvent Then Call SetToggleValues(Me.Controls("ToggleButton3"))
End Sub

Private Sub SetToggleValues(cControl As Control)
bExecuteEvent = False
For Each cColControl In colToggles
    If Not cColControl Is cControl Then cColControl.Value = Not cControl.Value
Next cColControl
bExecuteEvent = True
End Sub

combo
 
Many thanks for this combo. this is very helpful indeed.

There's just one thing, which I can't solve with this.... if a user presses one of the toggles so it's value is true, then presses that same toggle to switch it back to false, all the other toggles change to true.

Many thanks

Thom
 
In Sub SetToggleValues(cControl As Control) combined with collection of toggles you have a reference both to clicked button and the rest of buttons. This should be enough to set values of other buttons depending on cControl value, the code depends on what behaviour when the button is false. Without any action in this case, in the 'For' loop:

If Not cColControl Is cControl And cControl.Value = True Then cColControl.Value = False

combo
 
Many thanks again for this combo.

I have to admit i've not worked with collections before, but I can make sense of the above, so i'm going to apply it to a couple of other ideas i've had.

have a star
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top