Toggle Radio button inside a Radio group

BageraBagera USMember ✭✭

Hey,

How I can check and uncheck a radio button inside a radio group like the image example below

Best Answer

  • BageraBagera US ✭✭
    Accepted Answer

    This is how I did it:

    I used tag to know if this radio button checked before or not.
    False => It is not checked
    True => It is checked

    radiobutton.Tag = false;

    radiobutton.Click += SingleChoiceQuestionAlternativeClick;

    Then:

    `private void SingleChoiceQuestionAlternativeClick(object sender, EventArgs e)
    {
    RadioButton questionAlternative = sender as RadioButton;

            if (questionAlternative != null)
            {
                RadioGroup questionAlternatives = questionAlternative.Parent as RadioGroup;
    
                if (questionAlternatives != null)
                {
                    if (questionAlternative.Tag.Equals(false))
                    {
                        for (int i = 0; i < questionAlternatives.ChildCount; i++)
                        {
                            RadioButton childRadioButton = questionAlternatives.GetChildAt(i) as RadioButton;
    
                            if (childRadioButton != null)
                            {
                                childRadioButton.Tag = false;
                            }
                        }
    
                        questionAlternative.Tag = true;
                    }
                    else
                    {
                        questionAlternative.Tag = false;
                        questionAlternatives.ClearCheck();
                    }
                }
            }
        }`
    

Answers

  • BageraBagera USMember ✭✭
    Accepted Answer

    This is how I did it:

    I used tag to know if this radio button checked before or not.
    False => It is not checked
    True => It is checked

    radiobutton.Tag = false;

    radiobutton.Click += SingleChoiceQuestionAlternativeClick;

    Then:

    `private void SingleChoiceQuestionAlternativeClick(object sender, EventArgs e)
    {
    RadioButton questionAlternative = sender as RadioButton;

            if (questionAlternative != null)
            {
                RadioGroup questionAlternatives = questionAlternative.Parent as RadioGroup;
    
                if (questionAlternatives != null)
                {
                    if (questionAlternative.Tag.Equals(false))
                    {
                        for (int i = 0; i < questionAlternatives.ChildCount; i++)
                        {
                            RadioButton childRadioButton = questionAlternatives.GetChildAt(i) as RadioButton;
    
                            if (childRadioButton != null)
                            {
                                childRadioButton.Tag = false;
                            }
                        }
    
                        questionAlternative.Tag = true;
                    }
                    else
                    {
                        questionAlternative.Tag = false;
                        questionAlternatives.ClearCheck();
                    }
                }
            }
        }`
    
Sign In or Register to comment.