locked
Help with Radiobuttons RRS feed

  • Question

  • Hello,

    In my Windows Forms Application project, I have a groupbox with two radiobuttons. It also has a label.

    The groupbox is called groupBox_ScopeQuality and the radiobuttons are called radioButton_ScopeQuality and radioButton_NeedsImprovement. My idea is when the appropriate radiobutton is clicked, I want the corresponding text to be displayed in a label in the same form.

    The text associated to the first radiobutton is "Yes" and the second, "No".

    I followed a couple of article, but nothing is helping me. Can you show me how that can be done please?

    Saturday, August 26, 2017 6:22 PM

Answers

  • Hi Nikhil,

    >>My idea is when the appropriate radiobutton is clicked, I want the corresponding text to be displayed in a label in the same form.

    I guess you want to display some text when the radiobutton is selected, if so, try this:

            private void radioButton1_Click(object sender, EventArgs e)
            {
                if (radioButton1.Checked)
                {
                    label1.Text = "Yes";
                }
            }
    
            private void radioButton2_Click(object sender, EventArgs e)
            {
                if (radioButton2.Checked)
                {
                    label1.Text = "No";
                }
            }

    Please let me know if I misunderstand what you mean.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Monday, August 28, 2017 1:11 AM
    • Proposed as answer by Stanly Fan Tuesday, August 29, 2017 8:44 AM
    • Marked as answer by Nikhil Kenvetil Wednesday, August 30, 2017 7:59 AM
    Monday, August 28, 2017 1:11 AM

All replies

  • Hi Nikhil Kenvetil,

    In advance, sorry if I misunderstand your question/what you want to do.

    It seems to be an endless loop.
      (1) RadioButton.CheckedChanged > some string is set on Label.Text
      (2) Label.Text is changed > RadioButton.Checked is changed
    ---
    (1) leads to (2) and (2) leads to (1)... this is an endless loop, isn't it?
    Please explain the meaning of "The text associated to the first radiobutton is "Yes" and the second, "No"."

    Regards,


    Ashidacchi

    Saturday, August 26, 2017 10:56 PM
  • Hi Nikhil,

    >>My idea is when the appropriate radiobutton is clicked, I want the corresponding text to be displayed in a label in the same form.

    I guess you want to display some text when the radiobutton is selected, if so, try this:

            private void radioButton1_Click(object sender, EventArgs e)
            {
                if (radioButton1.Checked)
                {
                    label1.Text = "Yes";
                }
            }
    
            private void radioButton2_Click(object sender, EventArgs e)
            {
                if (radioButton2.Checked)
                {
                    label1.Text = "No";
                }
            }

    Please let me know if I misunderstand what you mean.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Monday, August 28, 2017 1:11 AM
    • Proposed as answer by Stanly Fan Tuesday, August 29, 2017 8:44 AM
    • Marked as answer by Nikhil Kenvetil Wednesday, August 30, 2017 7:59 AM
    Monday, August 28, 2017 1:11 AM
  • Hi Nikhil Kenvetil,

    If you are using VB.NET, please see the below sample:
         
    Here's code:
    ' --- RadioButton: CheckedChanged
    Private Sub radioButton_ScopeQuality_CheckedChanged(sender As Object, e As EventArgs) _
    	Handles radioButton_ScopeQuality.CheckedChanged
    	' --- change Label.text according to checked state of RadioButton
    	If (Me.radioButton_ScopeQuality.Checked) Then
    		Me.Label1.Text = "Yes"
    		Me.Label1.ForeColor = Me.radioButton_ScopeQuality.ForeColor
    	End If
    	If (Me.radioButton_NeedsImprovement.Checked) Then
    		Me.Label1.Text = "No"
    		Me.Label1.ForeColor = Me.radioButton_NeedsImprovement.ForeColor
    	End If
    End Sub
    I hope this would be helpful.

    Regards,

    Ashidacchi

    • Proposed as answer by Stanly Fan Tuesday, August 29, 2017 8:44 AM
    Monday, August 28, 2017 2:20 AM
  • The following is C#, converting to vb.net would work also.

    Create one CheckChanged event for both RadioButton controls. Add the following to display the text of the selected radio button.

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        label1.Text = groupBox2.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked)?.Text;
    }

    Or set the Tag property of each radio button say in form load then for CheckedChanged

    label1.Text = groupBox2.Controls.OfType<RadioButton>()
        .FirstOrDefault(rb => rb.Checked)
        .Tag
        .ToString();


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Stanly Fan Tuesday, August 29, 2017 8:44 AM
    Monday, August 28, 2017 2:13 PM
  • Sorry for not getting back early. Yes, this is what I wanted. Thanks for helping me out.
    Wednesday, August 30, 2017 8:00 AM
  • Hello Ashidacchi. I got the answer I was looking for. Thanks for looking into this :)
    Wednesday, August 30, 2017 8:02 AM