Tab index is not working on radio buttons

12,777

Solution 1

From How to: Set the Tab Order on Windows Forms (MSDN):

A radio button group has a single tab stop at run time. The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false.

In other words, what you're seeing is normal. Those "Yes/No" radio buttons are in the same group, and you can't tab between radio buttons in the same group. As you tab, you'll only focus on the currently selected one, then move to the next control on the form (in your case, a TextBox).

To work around this, you could place each radio button in its own container (such as a Panel), which means you'd have two "groups" each with one radio button. But then you lose the built-in functionality that automatically deselects one radio button when you select the other. Your user will be able to select both radio buttons, so you'd need to add some logic that disables the other. If you decide to try that, experiment with the radio buttons' CheckedChanged or Click / MouseClick events.

As Steve said, and as stated in the answer he linked to, the way it works out-of-the-box is expected behavior for Windows, so think twice before overriding it unless you have a good reason for doing so.

Solution 2

It worked for me!

first you have to create a method like this:

private void TabStopChanged(object sender, EventArgs e)
{
     ((RadioButton)sender).TabStop = true;
}

and then, put this in your Form_Load event:

private void Form_Load(object sender, EventArgs e)
{
   foreach (var item in this.Controls)
   {
      if (item.GetType() == typeof(RadioButton))
         ((RadioButton)item).TabStopChanged += new System.EventHandler(TabStopChanged);
   }
}
Share:
12,777
Agnieszka Polec
Author by

Agnieszka Polec

Updated on June 04, 2022

Comments

  • Agnieszka Polec
    Agnieszka Polec almost 2 years

    This is the part of my form that I am asking about

    enter image description here

    This is the tab index:

    enter image description here

    The problem that the tab goes from Farmer Audi Status to Yes, then to Ownder Bank Name instead of going to No

    please notice that the yes and no already have 0.1.6.0 and 0.1.6.1 respectively.

    could you help me please?

    Notice

    both radio buttons has TabStop property to True

  • Agnieszka Polec
    Agnieszka Polec over 9 years
    but the problem is not solved completely, when I start the form, nothing is selected, so the tab works good, but if I select the yes option, then I try the tab, the tab is not working, I mean it goes again to the next text box :(
  • CST RAIZE
    CST RAIZE over 8 years
    It works for me. :) If the radiobuttons are placed in groupbox or panel, then change the foreach loop like foreach(var item in this.groupBox1.Controls)
  • gridtrak
    gridtrak over 6 years
    This only works when 1 of the radio buttons is Checked. It does not work if none are checked.
  • saminpa
    saminpa about 5 years
    Adding, belatedly since it's not immediately obvious from the MSDN link, that the default keyboard shortcuts to move between grouped radiobuttons are the right/left arrow keys, NOT tab, for some reason. About as intuitive (to me) as using the spacebar to check/uncheck checkboxes but not radiobuttons (except if no button in a group is selected but then the spacebar has no further effect...)