سؤال

I have 2 radio buttons which are suppose to generate a rectangle of two different colours,simply meaning, when the user selects radio button one it should generate a red colour rectangle and if the user selects radio button two it should create a blue colour rectangle.

My problem is after selecting the radio button one (the rectangle gets created) and if the user selects radio button 2 it creates another red rectangle , then a blue rectangle (Which is correct), but the problem is when the user selects 2 it should not create another red rectangle. I guess there is a problem with unchecking, but i can't find a proper solution. Here is what i have done for the radio buttons :-

    private void rbOne_CheckedChanged(object sender, EventArgs e)
    {
        if (rbOne.Checked)
        {
            status = rbOne.Text;
            buff.write(Color.Red, status);
        }
    }

    private void rbTwo_CheckedChanged(object sender, EventArgs e)
    {
        if (rbTwo.Checked)
        {
            status = rbTwo.Text;
            buff.write(Color.Blue, status);
        }
    } 

What seems to be the problem here ?

Thank you for your time.

هل كانت مفيدة؟

المحلول

You have to check the radio button selection in both the radio button check change event and need to un-check the other one first. Have a look here

private void rbOne_CheckedChanged(object sender, EventArgs e)
    {
        if (rbTwo.Checked)
        {
         // make it uncheck  and remove the red rectangle
        } 
        {
        if (rbOne.Checked)
        {
            status = rbOne.Text;
            buff.write(Color.Red, status);
        }
    }

private void rbTwo_CheckedChanged(object sender, EventArgs e)
{
    if (rbOne.Checked)
    {
     // make it uncheck  and remove the blue rectangle
    } 
    if (rbTwo.Checked)
    {
        status = rbTwo.Text;
        buff.write(Color.Blue, status);
    }
} 

or either you can check the radio button checked property first for each of them if one is checked then don't create another rectangle.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top