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?

1 Answers

1
Mong Zhu On Best Solutions

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 e.g. 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.