une


Here is a little puzzle I cannot solve if you have the time and inclination to look at it. I have 3 toggle buttons whose captions and values I want to be interdependent as follows;

*When ToggleButton1 is clicked its caption alternates between ON and OFF and its value between True and False.
*When ToggleButton2 is clicked its caption alternates between ON and OFF and its value between True and False.
*When ToggleButton3 is clicked its caption alternates between ON and OFF and its value between True and False.

*If ToggleButton1 is clicked from ON to OFF (True to False) ToggleButton2 and ToggleButton3 will change to OFF (False)

*If ToggleButton2 or ToggleButton3 are clicked from OFF (False) to ON (True), ToggleButton1 will change to ON (True).
*If ToggleButton2 is clicked from ON to OFF (True to False) while ToggleButton3 is OFF (False), ToggleButton1 should change from ON (True) to OFF (False).
*If ToggleButton2 is clicked from ON to OFF (True to False) while ToggleButton3 is ON (True), ToggleButton1 should remain ON (True).
*If ToggleButton3 is clicked from ON to OFF (True to False) while ToggleButton2 is OFF (False), ToggleButton1 should change from ON (True) to OFF (False).
*If ToggleButton3 is clicked from ON to OFF (True to False) while ToggleButton2 is ON (True), ToggleButton1 should remain ON (True).

Here is my code that doesn't quite get there. The problem it has is that if ToggleButton1, ToggleButton2 and ToggleButton3 are ON (True) and then either ToggleButton2 or ToggleButton3 are clicked to OFF (False) ToggleButton1 changes to OFF (False), when I want it to remain ON (True) in this case;

Private Sub ToggleButton1_Click()
ToggleButton1.Caption = IIf(ToggleButton1.Value, "ON", "OFF")
If ToggleButton1.Value = False Then
ToggleButton2.Caption = IIf(ToggleButton1.Value, "ON", "OFF")
ToggleButton2.Value = IIf(ToggleButton1.Value, True, False)
ToggleButton3.Caption = IIf(ToggleButton1.Value, "ON", "OFF")
ToggleButton3.Value = IIf(ToggleButton1.Value, True, False)
End If
End Sub

Private Sub ToggleButton2_Click()
ToggleButton2.Caption = IIf(ToggleButton2.Value, "ON", "OFF")
If ToggleButton2.Value = True Then
ToggleButton1.Caption = IIf(ToggleButton2.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton2.Value, True, False)
Else
ToggleButton1.Caption = IIf(ToggleButton2.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton2.Value, True, False)
End If
End Sub

Private Sub ToggleButton3_Click()
ToggleButton3.Caption = IIf(ToggleButton3.Value, "ON", "OFF")
If ToggleButton3.Value = True Then
ToggleButton1.Caption = IIf(ToggleButton3.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton3.Value, True, False)
Else
ToggleButton1.Caption = IIf(ToggleButton3.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton3.Value, True, False)
End If
End Sub







Re: Toggle button logic problem

Andy Pope


Hi,

Use a variable to hold the fact that you are updating the toogle buttons.

Private m_blnUpdating As Boolean

Private Sub ToggleButton1_Click()

If m_blnUpdating Then Exit Sub
m_blnUpdating = True

ToggleButton1.Caption = IIf(ToggleButton1.Value, "ON", "OFF")
If ToggleButton1.Value = False Then
ToggleButton2.Caption = IIf(ToggleButton1.Value, "ON", "OFF")
ToggleButton2.Value = IIf(ToggleButton1.Value, True, False)
ToggleButton3.Caption = IIf(ToggleButton1.Value, "ON", "OFF")
ToggleButton3.Value = IIf(ToggleButton1.Value, True, False)
End If

m_blnUpdating = False

End Sub

Private Sub ToggleButton2_Click()

If m_blnUpdating Then Exit Sub
m_blnUpdating = True

ToggleButton2.Caption = IIf(ToggleButton2.Value, "ON", "OFF")
If ToggleButton2.Value = True Then
ToggleButton1.Caption = IIf(ToggleButton2.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton2.Value, True, False)
Else
ToggleButton1.Caption = IIf(ToggleButton2.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton2.Value, True, False)
End If

m_blnUpdating = False

End Sub

Private Sub ToggleButton3_Click()

If m_blnUpdating Then Exit Sub
m_blnUpdating = True

ToggleButton3.Caption = IIf(ToggleButton3.Value, "ON", "OFF")
If ToggleButton3.Value = True Then
ToggleButton1.Caption = IIf(ToggleButton3.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton3.Value, True, False)
Else
ToggleButton1.Caption = IIf(ToggleButton3.Value, "ON", "OFF")
ToggleButton1.Value = IIf(ToggleButton3.Value, True, False)
End If

m_blnUpdating = False

End Sub







Re: Toggle button logic problem

une

Thanks,
I will give it a try when I get back from my holiday. Got to pack now!