Many Toggle Buttons

  • Thread starter Thread starter Rachel
  • Start date Start date
R

Rachel

I have over two hundred toggle buttons on a form. Is there a way to write
"general" code rather than code each button that will turn the font red on the
button I turn on and turn the font back on the button I turn off?

I'm trying to save not having to code each button but have individual buttons
react to On and Off.

Thanks!

Rachel
 
Rachel,

Something like below will do what you're describing. It
just passes the control index of the toggle button to the
generic subroutine.

It wasn't clear to me if what you're doing would always
turn one button off when another is turned on. If that's
the case, you may wish to consider option groups.

Best of luck!

Private Sub ChangeColor(ctl As Control)
If ctl.ForeColor = 255 Then ' red
ctl.ForeColor = 0 ' black
Else
ctl.ForeColor = 255
End If
End Sub

Private Sub Toggle2_Click()
Call ChangeColor(Me.Controls(1))
End Sub

Private Sub Toggle1_Click()
Call ChangeColor(Me.Controls(0))
End Sub
I have over two hundred toggle buttons on a form. Is there a way to write
"general" code rather than code each button that will turn the font red on the
button I turn on and turn the font back on the button I
turn off?
 
This function will do the job:

Private Function ToggleColour(ID As Long)
Me("Toggle" & ID).ForeColor = -255 * Me("Toggle" & ID)
End Function

It assumes your toggle buttons are named "Toggle" then a number - eg
Toggle1, toggle2 etc. In the after update property for each toggle button
put "=ToggleColour(1)" (change the 1 for the number at the end of the
control name). You could write some code to do this for you.
 
Rachel said:
I have over two hundred toggle buttons on a form. Is there a way to write
"general" code rather than code each button that will turn the font red on the
button I turn on and turn the font back on the button I turn off?

I'm trying to save not having to code each button but have individual buttons
react to On and Off.

Create a public function:

Public Function ToggleClick()
IF Me.ActiveControl = True Then
Me.ActiveControl.Forecolor = vbRed
Else
Me.ActiveControl.Forecolor = vbBlack
End If
End Function

Then drag a selection box over all the toggle buttons and
set the OnClick property of all of them to:

=ToggleClick()
 
Back
Top