1
votes

I have a couple of RadioButtons on a C# Winforms project. I want to provide a "clear form" button which should reset the radiobuttons.

Currently I just set the RadioButton.Checked values to false and it appears to be fine.

rdbBlockRelease.Checked = false;
rdbRegularRelease.Checked = false;

The issue is when you start to tab through the form after clearing it.

Before you tab through the form, the tab order works as expected. If you check a radiobutton and then clear the form the radiobuttons appear to be removed from the taborder.

I believe this is because of how radiobuttons and tab orders work. In a radio button group, if all radio buttons are unchecked, the radio button with the lowest taborder in the group will gain focus when you tab to it.

If you check a radio button and then tab to the group, unchecked radio buttons appear to be skipped; the focus goes to the checked radio button.

Keeping this in mind, I believe what is happening the form thinks the radio button group still has a checked radio button and changing the radiobutton.checked values doesn't appear to affect that. So when the user tries to tab to the radio button group after clearing the form, the form says something like:

"Oh, okay. Give focus to the selected radio button. Oh, there's no selected radio button. Give focus to the next item in tab order."

Now comes my question. How do I prevent this? How do I make the user able to tab to the radio button group after clearing a form?

I'm using Visual Studio 2017.

2
@CraigW. it's not valid for a user to select neither option. I could put in a default value. (Craig's comment was apparently removed as I typed this in...)Tyler
It's in fact a bug! They tried to implement a feature, but they didn't write some code for cases that there isn't any checked radio. The expected behavior when there is no checked item, is focusing on the first one.Reza Aghaei

2 Answers

2
votes

They have written code to implement this feature, but the code has some bug:

When using tab key to move between group of radio buttons, it tries to select the first checked radio button. But when there is no checked radio button, it neglects the group.

There are some methods in RadioButton source code like PerformAutoUpdates and WipeTabStops which are responsible to implement the behavior of that Tab key. Those methods check for AutoCheck and they work only in case AutoCheck is true.

So if you want to change the behavior, it's enough to set AutoCheck property to false and handle click event of all radio buttons yourself and revert the Checked property yourself.

Yes, I know it seems irrelevant at first glance! But that's it.

private void radioButton_Click(object sender, EventArgs e)
{
    var radio = (RadioButton)sender;
    radio.Checked = !radio.Checked;
}

Note: Instead of above event handler, you also can create your own radio button deriving from RadioButton and set AutoCheck to false and override OnClick and set Checked= !Checked.

0
votes

While Reza's answer will allow you have no default value selected for a radio button group, you should still consider just having a default value.

As Craig W. mentioned, before deleting his comment, most forms will have one of the radio buttons selected by default. By doing like most, this problem can be prevented.