Radio Buttons & Check Boxes
The event generated when the user interacts with these controls is not a click event as it is when the user interacts with a button, but a CheckChanged event. This is because when the user clicks on these controls, they intend to change the current status of the control. A problem occurs when a beginning programmer, or one new to VB, assumes that a click means the user is selecting (turning on) that option. It is actually just as likely that the user is deselecting or turning off that option.
Think about the operation of a Check Box compared to a Radio Buttons. Check boxes have to be deselected by the program user whereas Radio Buttons are automatically deselected for the user when they click on a different radio button.
So, does clicking turn the check on or does it turn the check off? Truthfully it can do either one. If the check is currently on then clicking it will turn the check off and vica versa. This is precisely the reason why the procedure is named CheckChanged. It is changing the check from its current state to the opposite state. Radio buttons and Check boxes are controls which alter the state of something in a program. Buttons are controls which activate an event procedure. If a programmer forgets that the Radio button and Check box event is CheckChanged, their coding will generate peculiar and unreliable results. Here is a program you can download which demonstrates this behaviour.
The most important effect of this, is that when we code using these controls, we must not assume that the user event is turning the state of the control on, rather it is changing the state of the control. It might be turning the control off. The easiest way to cope with this ambiguity when coding is to always use a Forced Choice selection structure. Explicitly check the state of the control by testing the Checked property of the control. Then provide one set of commands to be executed if the control is being selected, and another set of cammands to be executed if the control is being deselected. This way we can code the event to respond appropriately whether it is turned on or whether it is turned off. Typical code would look like this:
If EmailOptIn.Checked then
optIn = True
optIn = False
This is better code - more reliable - than using just a conditional and incorrectly assuming that the user is always turning the control on. Making assumptions about the state of controls in programming always causes problems sooner or later.