473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with Radio Buttons

I need help with my radio buttons...
I am creating an application that dislays the number of daily calories needed to maintain your current weight.
The moderately active female will have total calories per day = weight * 12 calories
The relatively inactive female will have total calories per day = weight * 10 calories
The moderately active male will have total calories per day = weight * 15 calories
And the relatively inactive male will have total calories per day = weight * 13 calories
I have created a GUI interface with two group boxes. One group box contains two radio buttons Male & Female, the other group box contains Moderately & Relatively inactive radio buttons.
I have a label and a text box prompting the user for their weight, two labels to display the number of daily calories needed to maintain their weight, and three buttons (Calculate, Clear, and Exit)

I need to know how I can create a Sub procedure that tells me when each radio button is checked, and how I can incorporate the results into my If/ElseIf/Else code?
Feb 27 '06 #1
3 11622
If/Else code? I was thinking of something along the lines of...

Private Sub DetermineGenderAndActivityLevel()
'determines gender
Dim female As Integer
Dim male As Integer
Dim moderate As Integer
Dim relative As Integer
If Me.uiFemaleRadioButton.Checked = True Then
female = 1
Else
male = 2
End If

'determines activity level
If Me.uiModerateRadioButton.Checked = True Then
moderate = 3
Else
relative = 4
End If
End Sub

Private Sub ProcessGenderRadioButtons(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles uiFemaleRadioButton.Click, uiMaleRadioButton.Click
Call DetermineGenderAndActivityLevel()

End Sub

Private Sub ProcessActivityLevelButtons(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles uiRelativeRadioButton.Click, uiModerateRadioButton.Click
Call DetermineGenderAndActivityLevel()

End Sub
Feb 27 '06 #2
Banfa
9,065 Expert Mod 8TB
The code you have looks to me like it should work (entirely on the basis of the dabbling a did with VB 2 9 years ago) however please note that
  1. You have declared the variables female, male, moderate and relative locally to DetermineGenderAndActivityLevel. I think this means that they would not be visible (or even exist) outside this function.
  2. Even if they are available outside the function (or you change the code so that they are) why bother, you already have access to If Me.uiFemaleRadioButton.Checked and If Me.uiMaleRadioButton.Checked etc to get this data.
  3. To me it would be more logical to have a single variable to store the current selection from a set of radio buttons so rather than bith male and female variables a single gender variable that records the current gender.
  4. Your code assume that if 1 radio button is unchecked then the other one is checked, it may be that this has to be the case in your program but it is possible that neither radio button is check. For instance when the program starts uncheck both radio buttons to force the user to make a choice.
  5. Intercepting the Radio Button Activity event should not really be required if all you are doing is recording the state of the radio buttons as this can be done at the time that the data is required. You should use it if you want to perform some action that is unrelated to the radio button, for instance enable or disable another control on the dialogue box or automatically update the calculation.
Feb 28 '06 #3
Thank you for all your help Banfa! This really gave me a much better understanding of where I was going wrong in the code! :cool:

The code you have looks to me like it should work (entirely on the basis of the dabbling a did with VB 2 9 years ago) however please note that
  1. You have declared the variables female, male, moderate and relative locally to DetermineGenderAndActivityLevel. I think this means that they would not be visible (or even exist) outside this function.
  2. Even if they are available outside the function (or you change the code so that they are) why bother, you already have access to If Me.uiFemaleRadioButton.Checked and If Me.uiMaleRadioButton.Checked etc to get this data.
  3. To me it would be more logical to have a single variable to store the current selection from a set of radio buttons so rather than bith male and female variables a single gender variable that records the current gender.
  4. Your code assume that if 1 radio button is unchecked then the other one is checked, it may be that this has to be the case in your program but it is possible that neither radio button is check. For instance when the program starts uncheck both radio buttons to force the user to make a choice.
  5. Intercepting the Radio Button Activity event should not really be required if all you are doing is recording the state of the radio buttons as this can be done at the time that the data is required. You should use it if you want to perform some action that is unrelated to the radio button, for instance enable or disable another control on the dialogue box or automatically update the calculation.
Feb 28 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff | last post by:
I'm trying to create a dynamic form that can have multiple groups of radio buttons (each group has two buttons) with the same name. Essentially, the form allows a user to enter as many names as...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
6
by: Craig Keightley | last post by:
I have a page that has n number of radio groups (yes/No) how can i prevent the form being submitted if more than one radio group is not selected? By default all radio groups are unchecked ...
0
by: momo | last post by:
Hello all, I need some help with radio buttons. This is what I have. I have 15 groups of radio buttons and each group contains three radio buttons which non of them are selected. I need a way...
1
by: momo | last post by:
Hello all, I need some help with radio buttons. This is what I have. I have 15 groups of radio buttons and each group contains three radio buttons which non of them are selected. I need a way...
3
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way...
8
by: rajani gupta | last post by:
Hi i have this code to generate radio buttons in group echo "<b>$j.</b><b>&nbsp;$Ques</b><p><table class='text'><tr><td><input type=radio value=a name=$Q_ID></td><td>$opt1</td></tr><tr><td...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
3
by: norbit | last post by:
Hello, I’m new to learning JavaScript. I was trying out a simple script but I can’t seem to make it work: <HTML><HEAD><TITLE>Radio Buttons</TITLE> <SCRIPT language=JavaScript> function...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.