Juggling Code the Easy Way

Many controls act as a toggle switch--clicking the control simply reverses its current state. For instance, if the control is off (equals False), a click reverses the control's state to on (True). Likewise, if the control is on, a click turns it off. To use a toggle control, you simply add code that responds to the control's current state.

You might be surprised to learn that most of the time you won't need a lot of code for your toggle controls--that is, if you can reduce your control's purpose to a simple Boolean value (True or False). When this is the case, use the Not operator to switch between the two values. For instance, let's suppose that you want to toggle a control's Enabled property between True and False (on and off). To do so, use the Not operator in the form

control.Enabled = Not control.Enabled

where control represents the name or reference of your toggle control. If the control's current state is true and the control's Enabled property is on (True), then clicking the control reverses that situation by turning the Enabled property off (False). You're not limited to using this technique with properties, but the control's state must reduce to a Boolean value.

Access Index

Main Index

Search RD Techbase