Help with Radio Buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vanidosa27
    New Member
    • Feb 2006
    • 4

    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?
  • vanidosa27
    New Member
    • Feb 2006
    • 4

    #2
    I was thinking...

    If/Else code? I was thinking of something along the lines of...

    Private Sub DetermineGender AndActivityLeve l()
    'determines gender
    Dim female As Integer
    Dim male As Integer
    Dim moderate As Integer
    Dim relative As Integer
    If Me.uiFemaleRadi oButton.Checked = True Then
    female = 1
    Else
    male = 2
    End If

    'determines activity level
    If Me.uiModerateRa dioButton.Check ed = True Then
    moderate = 3
    Else
    relative = 4
    End If
    End Sub

    Private Sub ProcessGenderRa dioButtons(ByVa l sender As Object, ByVal e As System.EventArg s) _
    Handles uiFemaleRadioBu tton.Click, uiMaleRadioButt on.Click
    Call DetermineGender AndActivityLeve l()

    End Sub

    Private Sub ProcessActivity LevelButtons(By Val sender As Object, ByVal e As System.EventArg s) _
    Handles uiRelativeRadio Button.Click, uiModerateRadio Button.Click
    Call DetermineGender AndActivityLeve l()

    End Sub
    Last edited by vanidosa27; Feb 27 '06, 02:04 AM.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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 DetermineGender AndActivityLeve l. 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.uiFemaleRadi oButton.Checked and If Me.uiMaleRadioB utton.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.

      Comment

      • vanidosa27
        New Member
        • Feb 2006
        • 4

        #4
        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:

        Originally posted by Banfa
        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 DetermineGender AndActivityLeve l. 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.uiFemaleRadi oButton.Checked and If Me.uiMaleRadioB utton.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.

        Comment

        Working...