Written By TheVBProgramer.
Option buttons, also called radio buttons, are typically used in a group of two or more. At any one time, only one button in the group can be "on". Clicking an option button turns it "on" and turns all other buttons in the group "off".
Option button groups operate in a container control, such as a frame. Therefore, different sets of option button groups should be placed in their own frame on the form. Recall how to place a control directly inside a frame: either (1) single click it from the toolbox and draw it in, or (2) double-click it from the toolbox, cut it from the form, and paste it into the frame. If a group of option buttons is not contained within a frame, then the form itself acts as their container.
In code, to perform an action based on which option button the user clicked, do one of two things:
(1) To perform an action as soon as the user clicks an option button, place code in the Click event of the option button.
(2) To perform a "delayed" action, such as when the user clicks a command button, check the Value property of the option buttons in the group. If the Value = True, then the button is "on".
Examples using both methods follow. (Note: The examples in this topic use the built-in color constants vbRed, vbGreen, and vbBlue. More information on color constants is provided in the section at the end of this topic.)
First, a frame containing three option buttons, named optRed, optGreen, and optBlue respectively was placed on the form shown below:

Next, code was written for the Click event of each of the option buttons. The code causes the background color of the form to change to the appropriate color when one of the option buttons is clicked:
Private Sub optRed_Click()
Form1.BackColor = vbRed
End Sub
Private Sub optGreen_Click()
Form1.BackColor = vbGreen
End Sub
Private Sub optBlue_Click()
Form1.BackColor = vbBlue
End Sub
If the program was to be run at this point, you would get immediate results when you clicked one of the option buttons – as soon as you clicked one of the option buttons, the form would change color.
To demonstrate the "delayed" action, let's say the above program was modified as follows: First, a command button named cmdChangeColor is placed on the form:

Second, all code from the option buttons' Click event (shown in the previous example above) is removed.
Third, code for the cmdChangeColor_Click event is written as follows:
Private Sub cmdChangeColor_Click()
If optRed.Value = True Then
Form1.BackColor = vbRed
ElseIf optGreen.Value = True Then
Form1.BackColor = vbGreen
Else
Form1.BackColor = vbBlue
End If
End Sub
If the program was to be run at this point, clicking on one of the option buttons would not cause an immediate result – it would simply set the Value of the clicked button to True. Only when you clicked the "Change Color" button would the background color of the form change, based on the code above.
Syntax Notes:
The Value property is the default property of the option button and can therefore be dropped when coding, as in:
If optRed = True Then . . .
Since Value is a Boolean property, you can drop the "= True" from the conditional expression, making it possible to cut the coding down further to:
If optRed Then . . .
Coding an Option Button Control Array
Using the same scenarios as in the examples above, suppose we used a control array for the set of three option buttons (a control array called optColor indexed 0 through 2 instead of the individual buttons optRed, optGreen, and optBlue).
The coding for the "immediate" action (using the option button's click event) would be:
Private Sub optColor_Click(Index As Integer)
Select Case Index
Case 0
Form1.BackColor = vbRed
Case 1
Form1.BackColor = vbGreen
Case 2 ' could also use "Case Else" here
Form1.BackColor = vbBlue
End Select
End Sub
The coding for the "delayed" action (using a command button) would be:
Private Sub cmdChangeColor_Click()
Dim intLoopCtr As Integer
Dim intIndex As Integer
For intLoopCtr = 0 To 2
If optColor(intLoopCtr).Value = True Then
intIndex = intLoopCtr
Exit For
End If
Next
Select Case intIndex
Case 0
Form1.BackColor = vbRed
Case 1
Form1.BackColor = vbGreen
Case 2 ' could also use "Case Else" here
Form1.BackColor = vbBlue
End Select
End Sub
The sample program exercises each of the scenarios discussed above. A screen shot is shown below:

Download the VB project code for the example above here.
Color Constants
VB provides a set of built-in constants that can be used to refer to colors. Some of these were used in the sample program presented in this topic. The available constants, from MSDN, are listed below:
Colors
|
Constant |
Value |
Description |
|
vbBlack |
&H0 |
Black |
|
vbRed |
&HFF |
Red |
|
vbGreen |
&HFF00 |
Green |
|
vbYellow |
&HFFFF |
Yellow |
|
vbBlue |
&HFF0000 |
Blue |
|
vbMagenta |
&HFF00FF |
Magenta |
|
vbCyan |
&HFFFF00 |
Cyan |
|
vbWhite |
&HFFFFFF |
White |
System Colors
|
Constant |
Value |
Description |
|
vbScrollBars |
&H80000000 |
Scroll bar color |
|
vbDesktop |
&H80000001 |
Desktop color |
|
vbActiveTitleBar |
&H80000002 |
Color of the title bar for the active window |
|
vbInactiveTitleBar |
&H80000003 |
Color of the title bar for the inactive window |
|
vbMenuBar |
&H80000004 |
Menu background color |
|
vbWindowBackground |
&H80000005 |
Window background color |
|
vbWindowFrame |
&H80000006 |
Window frame color |
|
vbMenuText |
&H80000007 |
Color of text on menus |
|
vbWindowText |
&H80000008 |
Color of text in windows |
|
vbTitleBarText |
&H80000009 |
Color of text in caption, size box, and scroll arrow |
|
vbActiveBorder |
&H8000000A |
Border color of active window |
|
vbInactiveBorder |
&H8000000B |
Border color of inactive window |
|
vbApplicationWorkspace |
&H8000000C |
Background color of multiple-document interface (MDI) applications |
|
vbHighlight |
&H8000000D |
Background color of items selected in a control |
|
vbHighlightText |
&H8000000E |
Text color of items selected in a control |
|
vbButtonFace |
&H8000000F |
Color of shading on the face of command buttons |
|
vbButtonShadow |
&H80000010 |
Color of shading on the edge of command buttons |
|
vbGrayText |
&H80000011 |
Grayed (disabled) text |
|
vbButtonText |
&H80000012 |
Text color on push buttons |
|
vbInactiveCaptionText |
&H80000013 |
Color of text in an inactive caption |
|
vb3DHighlight |
&H80000014 |
Highlight color for 3D display elements |
|
vb3DDKShadow |
&H80000015 |
Darkest shadow color for 3D display elements |
|
vb3DLight |
&H80000016 |
Second lightest of the 3D colors after vb3Dhighlight |
|
vb3DFace |
&H8000000F |
Color of text face |
|
vb3Dshadow |
&H80000010 |
Color of text shadow |
|
vbInfoText |
&H80000017 |
Color of text in ToolTips |
|
vbInfoBackground |
&H80000018 |
Background color of ToolTips |
Comments
option button
olivera
how to select 2 option button in 1 form??
expamle
i got 5 option button .. i want to select 2 or 3... how to do this??
It cancels my form
I'm using the radio controls to select either player one or two for a score board.
When im using the code Total1 = 1 + Total1 and run the program, it ends my form.
RadioButton1.Checked = True
TextBox4.Text = Total1
On the button Total1 is my integer and when the radiobutton1 is selected i wanted the counter "1" to be displayed in text box 4 but it just close's everything down and dosen't show any error's.
Is there a tutoral i could look at or any idea's round this?
how can i count all the
how can i count all the radio buttons that are checked? please reply asap.
ho to select option buttons
hi , in my form i have three frames each containing some option buttons, by default i have to keep the fist option clicked(value="true") in each frame, but when i put the value say option1 in fram1 as TRUE and then put value say option6 in frame2 as TRUE then the 1st one becomes False, when i put again TRUE for value in next optionbutton in fram3 this second one becomes false.
Please suggest how to keep one option in each fram selected at one time(By Default Option.Value should be TRUE for first option Button in each frame.).
Options buttons & IF FUNCTION
Anyone know how to use an option button in conjunction with an if funciton.
Here is the scenario...
Column A = Option button 1 (TRUE)
Column B = Option buttion 2 (FALSE)
Column C = If function, where if column A is True, then ....."output"
I've already ensured the T/F option buttons are in their own groups but now when i write my logical test, i dont get back the proper results..Any thoughts?
if it's two groups
spouses I've got 2 group of RadioButton how i can connect between them i mean by using (IF .. Then)
For example If RadioButton1 & RadioButton2 = true then (blah...blah)
Put your radiobuttons into 2
Put your radiobuttons into 2 different Frames.
Check box and option button
I write a VB program like this:
here is check box
here is option button1
here is option button2
here is option button3
Supposed to be, when i click the checkbox, the 3 option button should be in true value, meaning all 3 have dot value. the problem is
only the 3rd option button has the true value. Can you give me the program of this one. just send your replay to my email address. Thanks
Value not valid
I am writing a program where there are 10 option buttons.
When I give if optionbutton1.value then
Msgbox "blah blah "
elseif optionbutton2.value then
Msgbox "blah"
end if
it is coming invalid function or variable.
Pls help me in this. pls
option button
you must specify if it is true or false. ex. me.option1.value = True
try, if optionbutton1.value
try,
if optionbutton1.value = true then
option Botton
This is good artical.I get lot of things about op. botton.Thanks .Wish U all the best
thx
Thanks! I couldn't figure out how to use these "radio buttons" before I read this tutorial! I used to think it was something like...
If Option1.Selected = True Then ...
LOL
excellent!!!!
I Luv It!!!!!.......thanks for the resources!....
How to write codes for a search button.
How to write codes for a search button.
need ur assistance on how to programme a textbox and command button such that the user will type in an existing name in a database and when the cmdbutton is clicked, the name and record will be displayed
Option buttons
Im doing a game with option buttons, but an option button at the top left hand corner get selected by default and stuffs it up. How do I stop that? Plz i relly need help!!
Option button default
modify the option button property value = false. You can do it with all your option buttons. To choose default, in declarations- general insert line with "option button".value = true
Very Helpful...
Wow, honestly, I never understood Radio buttons untill i just read that. I knew how they worked, but I couldn't get them to work.
Thanks a lot for this explanation, it really helped me.
Mac --- 17 Years Old from NewYork.
Current Project - A TextBased RPG, One Player and Offline.
Completed Projects - 41.
Future Projects - Probably some simple games, such as Minesweeper, Memory games, etc.
vb Optionbox
is it possible for me to connect a database using option box?
radio button
When the form loads, an option button gets selected by default? I don't want any of them to be selected by default.....how do i do that?????
answer
i think u can set the option button enable propity to false then it will not be selected :)
option buttons
how can i add an option buttons in a different groups wherein in that different group of option buttons is i can choose one option in that particular group and also i can choose another option in a different group.... how to do this in visual basic????
Option button by groups
Step 1. Create a container control (like a frame) inserted in your form.
Step 2. create one or more option buttons INSIDE a frame (or container control) clicking over the frame.
To create another group, follow the same steps.
For each container control, with several option buttons, only activated one at time.
Null
If no button is pushed for an option button, is it given a "null" value?
How can an option button be
How can an option button be made to work in an excel spreadsheet, this code does not perform the function for me. any HELP?
Excel and radio buttons
Just today I found a method to use radio buttons to plug values into excel cells.
I used the cell method.
For an example, create four radio buttons and make sure they are in the same group. Then code:
Private Sub OptionButton1_Click()
Cells(1, 1) = 1
End Sub
Private Sub OptionButton2_Click()
Cells(1, 1) = 2
End Sub
Private Sub OptionButton3_Click()
Cells(1, 1) = 3
End Sub
Private Sub OptionButton4_Click()
Cells(1, 1) = 4
End Sub
Clicking each button changes the value in cell a1 aka...$a$1, and (1,1)
That should get you started.
James
Trying to set an option
Trying to set an option button for a YES and No option. can't simply relate the code to this.. any HELP?
Setting an option
For you to set a no and a yes option you use the if statement and on the form there must be two option boxes. For example if you have one of the option boxes as optyes and the other as optno. Double click the two option boxes eg if you click the yes option box the code will be as follow
if optyes.value = 1 then
here you write what you want to be displayed when the yes option box is clicked.
else
here you write what you want to be displayed if the yes option box is not clicked.
end if
You also do the same for the No option box only this time you replace the yes word with no
I hope i've helped.
how to revert back to default settings
suppose if i change the background to red and want to change back to the previous default setting then what i have to do..
Reverting back to default setting
you first need a command button after clicking on the new command button in the code section write
BackColor=vbButtonFace
What about default settings?
Okay as far as it goes, but it doesn't say anything about how to set one of a set of buttons as the default (i.e. that which is selected when a program starts. That looks like pretty basic information to me, yet I can't find anything about it here!
Okay as far as it goes, but
Okay as far as it goes, but it doesn't say anything about how to set one of a set of buttons as the default (i.e. that which is selected when a program starts. That looks like pretty basic information to me, yet I can't find anything about it here!
The TabIndex property decides order the objects are selected in the form. You can see this, when the user press the Tab key. That means, the object with the TabIndex set to 0 will be the "default" selected. You can either assign the 0 TabIndex to another object (like a CommandButton or a TextBox) or to a new object and hide it (Visible property set to False).
After reading this tutorial
After reading this tutorial i learnt how option buttons using control arrays work for delayed action ie via a command button which i failed to do earlier. This tutorial taught that extra to me excellently.
Post new comment