Reputation: 101
I am currently working on a daily calorie calculator in VB.NET. My goal is to create an application that displays the number of daily calories a person needs to maintain his or her current weight. The number of calories is based on the person’s gender, activity level, and weight. The problem I am running into is being able to select multiple radio buttons. I know I must have to group the radio buttons into Gender and Activity level but I am not quite sure how to go about declaring and executing that. Here is my code so far...
Public Class dailyCalories
Dim Weight As Integer
Dim dailyCalories As Integer
Dim Male As RadioButton
Dim Female As RadioButton
Dim Active As RadioButton
Dim Inactive As RadioButton
Private Sub calculateDailyCalories()
Weight = CInt(txtWeight.Text)
If RbFemale.Checked And RbActive.Checked Then
dailyCalories = Weight * 12
End If
If RbFemale.Checked And RbInactive.Checked Then
dailyCalories = Weight * 10
End If
If RbMale.Checked And RbActive.Checked Then
dailyCalories = Weight * 15
End If
If RbMale.Checked And RbInactive.Checked Then
dailyCalories = Weight * 13
End If
End Sub
Private Sub displayDailyCalories()
txtDailyCalories.Text = dailyCalories
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Call calculateDailyCalories()
Call displayDailyCalories()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Upvotes: 0
Views: 6815
Reputation: 261
RadioButtons are used for a single choice, if you want to select more than one choice try to use CheckBoxes. if you want to use RadioButtons instead of CheckBoxes, you should group them by using a Panel or GroupBox Controls.
Upvotes: 1
Reputation: 8517
In Windows Forms, the RadioButton controls are grouped according to their enclosing control. To create multiple groups of RadioButtons, you would need to add containers such as Panel or GroupBox and then put RadioButtons inside.
After that, if user want to select radio button from each group, the user can able to select one radio button at a single time from one group.
Group boxes are container widgets that organize buttons into groups, both logically and on screen. They manage the interactions between the user and the application so that you do not have to enforce simple constraints. Group boxes are usually used to organize check boxes and radio buttons into exclusive groups.
It will look like this on screen:-
For more information refer to http://www.functionx.com/visualc/controls/radiobuttons.htm
Upvotes: 0
Reputation: 389
use the GroupBox from toolbox and place there your radiobuttons
Upvotes: 3