what i have done is copied answer in label and now matching the radiobutton option with it so far so good but i dont know how to uncheck radio button when i get to next question and method of mine stucks in between like sometime it just donot match the answer at all my code is below,. any other method that can help needed thanx

 if (radioButton1.Checked)
            {

                if (labelanswer.Text == radioButton1.Text.Trim())
                {

                    MessageBox.Show("right");

                    score = +score + 1;
                    labs.Text = score.ToString();

                }
                else if

                     (radioButton2.Checked)
                {

                    if (labelanswer.Text == radioButton2.Text.Trim())
                    {

                        MessageBox.Show("right");

                        score = +score + 1;
                        labs.Text = score.ToString();

                    }

                }
                else if

                    (radioButton3.Checked)
                {

                    if (labelanswer.Text == radioButton3.Text.Trim())
                    {

                        MessageBox.Show("right");

                        score = +score + 1;
                        labs.Text = score.ToString();

                    }
                    else if

                    (radioButton4.Checked)
                    {

                        if (labelanswer.Text == radioButton4.Text.Trim())
                        {

                            MessageBox.Show("right");

                            score = +score + 1;
                            labs.Text = score.ToString();

                        }

                    }

Dani AI

Generated

The main problem is the control flow: several of the radio-button checks are nested inside other branches, so most checks never run. That explains the intermittent mismatches. 's remark about formatting is relevant — clearer, flatter logic makes bugs obvious — and is correct that Checked can be changed in code; a consistent pattern is safer.

A reliable pattern: (1) find the one checked RadioButton, (2) compare it to the canonical answer (use a separate variable or the RadioButton.Tag rather than a visible label), (3) update score with score++ or score += 1, and (4) clear all options before loading the next question. Example helper methods:

private RadioButton GetSelectedRadioButton()
{
    foreach (Control c in groupBoxAnswers.Controls)
        if (c is RadioButton rb && rb.Checked)
            return rb;
    return null;
}

private void SubmitAnswer()
{
    var selected = GetSelectedRadioButton();
    if (selected == null) { MessageBox.Show("Select an option."); return; }

    // Option: compare text (trim + ignore case)
    bool correct = string.Equals(selected.Text.Trim(), correctAnswer.Trim(), StringComparison.OrdinalIgnoreCase);

    // Alternative: set selected.Tag = "correct" when loading the question
    // bool correct = (selected.Tag as string) == "correct";

    if (correct) { score++; labs.Text = score.ToString(); MessageBox.Show("Right"); }
    else { MessageBox.Show("Wrong"); }
}

To clear selections before the next question:

foreach (Control c in groupBoxAnswers.Controls)
    if (c is RadioButton rb) rb.Checked = false;

Notes: keep the correct answer out of any visible label (use a private variable or Tag), use Trim + case-insensitive comparison to avoid whitespace/case mismatches, give controls descriptive names, and prefer a data model (list of questions with correct index) instead of hard-coded if chains. This both fixes the logic bug and makes the quiz flow much easier to maintain.

Recommended Answers

All 4 Replies

While your code formatting leaves much to be desired, to check/uncheck has a lot of priors at https://www.google.com/search?q=uncheck+radio+button+in+c%23

All those blank lines are distracting and have me scroll up and down so I'll ask you try harder on the formatting.

The radiobutton Checked property can be set to true or false in your program.

is there anyother way to match answers ?

Yes, perhaps something like this:

A question
1.answer 1
2.answer 2
3.answer 3

Then in a textbox let the user answer 1,2 or 3, process this answer further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.