radio button for some question and display what i checked on a label

master_2013

Active member
Joined
Jul 8, 2013
Messages
41
Programming Experience
Beginner
i want to use several radio buttons in a group box and there are some question when it is checked whatever it checked that display one by one on a label

like

1: are u a gamer radio btn yes radiobtn no if checked yes it displays on label
2: are u a painter radio btn yes radiobtn no if checked yes it displays on label like "gamer + painter" or "no gamer+painter" or "no painter + gamer" or "no painter + no gamer"
and so on


here is my code when i checked the first radio button it moves automatically to last radio button


Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
Label26.Text = "Heart problem"
Else
RadioButton2.Checked = False
Label26.Text = "No Heart Problem"
End If


If RadioButton4.Checked = True Then
Label26.Text = "Heart Stroke"
Else
RadioButton3.Checked = True
Label26.Text = "No Heart Stroke"

End If

If RadioButton6.Checked = True Then
Label26.Text = "Epilepsy"
Else
RadioButton5.Checked = True
Label26.Text = "No Epilepsy"

End If

If RadioButton8.Checked = True Then
Label26.Text = "Pregnant"
Else
RadioButton7.Checked = True
Label26.Text = "No Prgnancy"
End If

If RadioButton10.Checked = True Then
Label26.Text = "Diabetes"
Else
RadioButton9.Checked = True
Label26.Text = "No Diabetes"

End If

If RadioButton12.Checked = True Then
Label26.Text = "Chest Pain"
Else
RadioButton11.Checked = True
Label26.Text = "No Chest Pain"
End If

If RadioButton14.Checked = True Then
Label26.Text = "Bone & Joint Pain"
Else
RadioButton13.Checked = True
Label26.Text = "No Bone and Joint Pain"
End If


If RadioButton16.Checked = True Then
Label26.Text = "Blood Pressure Problem"
Else
RadioButton15.Checked = True
Label26.Text = "No Blood Pressure Problem"
End If


If RadioButton18.Checked = True Then
Label26.Text = "High Cholesterol problem"
Else
RadioButton17.Checked = True
Label26.Text = "No High Cholesterol problem"
End If

If RadioButton20.Checked = True Then
Label26.Text = "High Cholesterol problem"
Else
RadioButton19.Checked = True
Label26.Text = "No High Cholesterol problem"
End If

If RadioButton22.Checked = True Then
Label26.Text = "Female Above 32 Years"
Else
RadioButton21.Checked = True
Label26.Text = "Less Than 32 Years"
End If

If RadioButton24.Checked = True Then
Label26.Text = "Exercising Less Then 1 Hour per Week"
Else
RadioButton23.Checked = True
Label26.Text = "Exercising More Then 1 Hour per week"
End If
End Sub
 

Attachments

  • gyyym.jpg
    gyyym.jpg
    117.7 KB · Views: 42
Hi,

Your problems all arise due to the fact that you are explicitly setting every RadioButton the first time that RadioButton1 is clicked. In addition to this you always overwrite your label text rather than building the Label's text depending on the options selected.

What you need to do is NOT set every RadioButton when RadioButton1 is clicked but to check the Checked property of every RadioButton and build the Label's text as you need. You must also do this for the CheckedChanged Event for ALL the RadioButtons and not just RadioButton1.

Assuming you have followed the instructions given in previous posts, I would add some text to the Tag Property of every RadioButton to describe what that RadioButton represents, i.e "Heart Problem", No Heart Problem" etc. Then I would add an event which Handles EVERY RadioButtons CheckChanged event. Once the CheckChanged event for any RadioButton is fired, Loop through the collection of Panels (one panel holds two RadioButtons), then loop through the collection of RadioButtons in each panel checking its Checked property. If the Checked property is True then add that RadioButtons Tag property to the Label''s text.

Here is an demonstration:-

VB.NET:
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton4.CheckedChanged, RadioButton3.CheckedChanged, RadioButton2.CheckedChanged
  Label1.Text = String.Empty
  For Each myPanel In Me.Controls.OfType(Of Panel)().OrderBy(Function(x) x.TabIndex)
    For Each myRadioButton In myPanel.Controls.OfType(Of RadioButton)()
      If myRadioButton.Checked Then
        Label1.Text &= myRadioButton.Tag.ToString & Environment.NewLine
      End If
    Next
  Next
End Sub

Hope that helps.

Cheers,

Ian
 
Instead of 2 radio buttons for each question, use a single checkbox. If the box is checked, then add the comment to a listbox instead of a label.
 
Back
Top