Using Option Buttons aka Radio Buttons

Level:
Level1

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

 

If you enjoyed this post, subscribe for updates (it's free)

radio buttons

how would i get this to pop up in another window e.g. i want the user to answer a series of questions but not on the main form

Multiples Choices using Radio Buttons

How do you choose multiple choices using radio buttons?

option button

What is the code for inserting a color e.g green in an option button such that when you click on the button,the color changes into green instead of remaining black.

Radio Button

If you have created a calculator and have used radio buttons such as "Calculate" and "Exit". How do I create the operator in order to be able to use the button?

Re order options control

I have 10 option controls refer to a cell, result a number of which option selected.
I want to reorder this option control, but how?
Thanks for your help

Radio buttons

I have to put together an evaluation instrument using Word 2007. I am not a code specialist, so can you help me write code for the buttons?

example

1. Employee is punctual 4 3 2 1 0
2. Employee is professional 4 3 2 1 0
3. Employee is courteous 4 3 2 1 0

Each numeric option needs to correspond to a radio button and I will need to create this in chart form.

If you could help me with this code, I think I could copy/paste/modify to complete all sections of the survey.

Any time and assistance that you can give me will be very much appreciated.

Thank you,

Rich

Option if then

Hi, i need to know how could i code for an if then with an option and a textbox!

What i want? Code or ideas for code to do this:
if opt1.enabled = true then opt2.enabled = false
if opt1.enabled = True then txt1.text = "V-"

I try it on VB6 but the syntax is invalid, second, i need in the textbox after V- appears the cursor but it disappear, how can i get the cursor right after opt1 is selected it appears after text V- in the textbox??

and last, i have a combobox, i put the list and locked is false, how can i capture what i select from the list in my databse?

I´m a beginner and i have to do this for my class!!

haii!! im an IT student ..

haii!! im an IT student .. could someone please help me do some coding about a voting system ?? i badly need ur help

Pizza Ordering Machine

there are 5 group box in each group box there are 5 check box and each of them has a equivalent value if ordered. How can i create a code for those ones. Example: 1st groupbox i chose button 3 which is equivalent to $500 then in groupbox2 i checked button 1 which is equivalent to $900 and last Groupbox3 i checked button 5 which is equivalent to $50. What will i put to compute button to input in the textbox1 the total amount of charge.

helppp!

Hey there! I am new to visual studio, and i have a project that is due and i need help to get it started i have some coding and can get the insults to the message box but that is it.

1. The interface must include:
• A textbox for the user to enter the name of the victim
• A control that allows the user to select the number of adjectives to be included in the insult (1-3)
• A button to generate the insult
• An output textbox where the insult will be displayed to the user
• A button to clear the application so you can generate new insults with a new victim
• A button that will display “about” information in a popup
2. The output textbox should display all insults generated, separated by a carriage return; every time the user clicks the generate button a new insult is added to the textbox
3. An error message box should appear if the user tries to generate insults without entering a victim's name first and not produce an insult as a result
4. The generator must randomly select each adjective and the single noun from a possible set of adjectives and nouns; there must be at least 10 possible adjectives and 6 possible nouns; repeated adjectives are allowed
5. After randomly selecting the adjectives and noun, the generator then builds an insult including the victim's name entered and displays in the output textbox. Every newly generated insult must be displayed at the top of the textbox – not the bottom!
6. This application must include some custom visual design (size, button size, fonts, form icons, colors, etc) as well as be user-friendly!
7. All controls must be logically named
8. You should not have any large blocks of code that are repeated. This is known as redundant code...however, handling multiple insult adjectives in this project tends to take beginners down this route. Can you use loops to avoid this?
9. In the past, some students have explored Arrays when developing a solution for this project. Although they do provide an efficient solution to the problem, this project requires that NO ARRAYS are used.

this is the information! i got the form all layed out and trying to get the victims to go with the radio button, but when i do that and typ my victim in and click the radio button only the insults show up..

Push button - Set commands

Hi,
Can anyone help me with setting up a push button to create and execute a push button that replaces old values with new values.

Example:

Old New
2.5 3.0
1.5 3.5
2.0 2.5

Then RESET button pressed.

Old New
3.0
3.5
2.5

So then, I can enter the new values.

Thanking you in advance for your time.

help!!

how can I do option button works?? because I use the code but is failed,I don't know :S

Do what....

What exactly do you want to do with the option button?

My e-mail is: chrzgt7@hotmail.com. Send me any questions there.

ChrisGT7

how do i display the option

how do i display the option in the data report??

How to insert radio button value into mysql database

for example:
if user are check in..
> click radio button and system will save the time when are the user check in..same goes to the check out method..
anyone out there..can you help me..

hellow world

ahm.. i would like ask some vb codes about option button..and check button connecting to database..if you can help me pls.. send me some example codes for that..tnx

HI!!!

Can anyone tell me how to fix this problem I have with my Radio Button: rad = radio btn = button

Dim txtNameEmp, txtworkdone, radbtnJ, radbtnA, radbtnT As String
Dim txtHours, lblPay As Double
If radbtnJ = True Then
lblPay = txtHour * 12 (<- Money they are suppose to earn for each hour)
ElseIf radbtnA = True Then
lblPay = txtHours * 10
ElseIf radbtnT = True Then
lblPay = txtHours * 8
End If
End Sub

I need to calculate how much an employee will earn if he works as: "J" = Permanent worker $12, "A" = apprentice workers $10 and "T" = Temporary workers $8. I putted the 3 radio buttons in a Panel (container) and kept trying to figure out what is the problem here but I don"t know... It keeps saying: "Variable 'radbtnJ' is used before it has been assigned a value. A null reference exception could result at runtime" Can anyone help please!!!

Variables

I believe the problem is at the way you have declared your variables.
You have only declared the "radbtny" as string and lblPay as Double. All the rest variables have been declared as variant and the program doesn't know what type is going to be used for. So when you try the "If radbtnJ = True Then" is a run-time error because the variable radbtnJ is empty and it hasn't been assigned any value to it yet.

Try this instead:

Dim txtNameEmp as String, txtworkdone as String, radbtnJ as String, radbtnA as String, radbtnT As String
Dim txtHours as Double, lblPay As Double

I hope I helped you!

option and checkbox w/ images

how can a make a program wherein there are 5 option buttons w/ diffrent pictures each behind it, and when i click 1 opt button the picture behind it will appear and so on.. pls answer.. tnxmuch.. and give me also the codes..

Code for Option buttons

Two control arrays (one with 5 option buttons and one more with 5 images) will help you enough.
Here is what I have in mind:

Private Sub Option1_Click(Index As Integer)
for I = 0 to 4
Image1(I).visible = iif(I <> Index, False, True)
next
End Sub

If you need anything else, please tell me!

hey!

Can you help me with my problem please! I posted it under my name Carla ♥... It seems like you could really help!!

hey guys, I have a problem

hey guys,

I have a problem in visual basic about in pizza ordering system

Can you send me a codes ..

Please reply ..

check box...

hey...

v are making a project name quiz....
v are making a form in which v are providing 8 check boxes for options to the question...
my query is...if user select any check how v store d value of check box and which text box user select it
displayed on text box...that user selects option no1,2,3 like that...

please send me the code....

Use a control array

You should make an array of checkboxes. Here is what you have to do:

Create a checkbox, copy it and paste it. Answer yes to the question so VB can make an array of checkboxes. Paste it as many times as you need. Place them at the order you want. After this, go and correct the Index property of each checkbox. This is the value you need. Double click any checkbox and the code should be something like this:

Private Sub Check1_Click(Index As Integer)
If Check1(Index).Value Then Text1.Text = Index
End Sub

Option Button Matrix 2 x 4

I am trying to develop a program that begins with a questionnaire with a set of 24 question. For each question, there are four option descriptors. The person answering the questionnaire is supposed to indicate which option Best describes him/her and which option Least describes him/her. For example:

Best Least Description

___ ___ Fair, just
___ ___ Pretty/Handsome
___ ___ Rich and Wealthy
___ ___ Liked and Admired

I want to use Option Buttons in this 2 x 4 array. Only one button can be 1 (true) in either of the vertical columns and only one be 1 (true) in any of the rows, although both buttons can be 0 (false) in any two of the rows. I can construct the matrix that works on the Row level, but not the Column level...or vice versa, but not both rows AND columns at the same time.

Any ideas?

check box...

hey...

v are making a project name movie ticket booking....
v are making a form in which v are providing 140 check boxes for selecting the seat...
question is...if user select any check how v store d value of check box and which text box user select it
displayed on text box...that user select seat no1,2,3 like tha...

plese send me the code....

Savesettings and getsettings with options buttons

please i need a code on how to use the savesettings and getsettings, with option buttons
i need it seriously for an upcoming test, please help me out

vb10

hey i hv vb 10..
i m doin a projct on hospital mangmnt in vb..i hv created forms as per my design of front end...i hv created database in vb 10 only wich is available...the problm is dat wen i run the form,and insert values and save dem,dey donot get actuly saved in d database...in short how to connect my database to my values...i hav add ,del ,edit ,save buttons on forms rest all labels n text boxes..the four buttons are not helpn me in connectng my database,pls help

using operation

hello i just want to learn option button using the operation please help me to know that...

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??

option button

Use check boxes -- the user can make multiple selections; don't use option buttons since the user can only make one selection

you can..

use frames for your option buttons.

i think you cant do

i think you cant do that...why not use checkboxes for that? ^_^

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

Value not valid

Try this:

If OptionButton1.Value = True Then
MsgBox "You chose Option 1"
ElseIf OptionButton2.Value = True Then
MsgBox "You chose Option 2"

End If

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

Search button code

Dim PassSQL As String
PassSQL = "SELECT*from Details WHERE[Name]like" & txtpassword.Text
mydetails.MoveFirst
Do Until mydetails.EOF 'Search untill the end of file records.
If txtpassword.Text = mydetails.Fields("Name") Then
Me.Hide
Form2.Show
Form2.txtname.Text = mydetails.Fields("Name")
Form2.txttel_no.Text = mydetails.Fields("Tel_no")
Form2.txtoccupation.Text = mydetails.Fields("Occupation")
Form2.txtresidence.Text = mydetails.Fields("Residence")
End Sub
Else
mydetails.MoveNext
End If
Loop
MsgBox("Invalid Name")

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.