简体   繁体   中英

Check if RadioButton is Checked from Checkchanged event

I need to check if the RadioButton is checked from the checkchanged event

private void radioButton6_CheckedChanged_2(object sender, EventArgs e)
{
    if(radioButton6.Checked)
    {

    }
}

Is this the properway to do it? Can i access the CheckBox state from the sender object?

Is this the properway to do it?

That depends. If you need each radio button to be very much distinguished. Meaning: You really need to check inside the event method:

if(radioButton6.Checked)

Then this is the way to go. Another case where this can be applied is when you have registered an individual event handler for each radio button. Then you can access the button directly, because in this case the sender will always be eg radioButton6 .

Can i access the CheckBox state from the sender object?

Yes you can. It might be adviseable in this case: If you decide to register the same method to a bunch of different radio button events then you could use the sender and cast it. This might save you a lot of double code.

private void radioButton6_CheckedChanged_2(object sender, EventArgs e)
{
    RadioButton button = sender as RadioButton;
    if(button?.Checked == true)
    {

    }
}

In such a case you can use the Tag property of the RadioButton to distinguish them. Set it in the beginning and then you can check for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM