2

Since TabStop does not work on RadioButtons (see linked question), how can I prevent a (WinForm) RadioButton from being tabbed into, but also allow the user to click on the RadioButton, without the tab focus jumping somewhere else.

I've read this and so I thought the following would work:

        rbFMV.Enter += (s, e) => focusFirstWorkflowButton();
        rbFMV.MouseUp += (s, e) => rbFMV.Focus();

But it doesn't. When I click on the RB, the focus jumps away, and does not come back on Mouse Up.

Any dirty workarounds out there?

Community
  • 1
  • 1
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • Sounds like a hack to account for some other deficiency in your design. Don't punish your users for your mistakes please. – Ed S. Jun 15 '11 at 21:20
  • Is that some kind of joke? The UI is very basic; the radio buttons are right where the users wanted them—they just don't want to tab into them. The only deficiency is in Microsoft's RadioButton not having a functional TabStop property – Adam Rackis Jun 15 '11 at 21:34

3 Answers3

2

Try something like this:

Set TabStop property of the radiobuttons to "false" in the form's constructor. Then attach the following events handlers to the CheckedChanged events of the radiobuttons.

public partial class Form1 : Form
{        
    public Form1()
    {
        InitializeComponent();
        radioButton1.TabStop = false;
        radioButton2.TabStop = false;
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        radioButton1.TabStop = false;
        radioButton2.TabStop = false;
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        radioButton1.TabStop = false;
        radioButton2.TabStop = false;
    }

}

You can attach these event handlers using lambda aswell, as you have shown in your question.

But the important point here is that whenever a radiobutton is checked/unchecked, it's tabstop property is also modified simultaneously. Hence you need to set it to false everytime that event occurs.

Hasan Fahim
  • 3,875
  • 1
  • 30
  • 51
2

The underlying Win32 RadioButton does not automatically change the TabStop property. However, if you use .NET Reflector you can see that the .NET control runs code to update the TabStop property whenever OnEnter method is called because focus has entered the control or whenever the AutoCheck or Checked properties are modified.

Luckily there is a simple solution to your problem. Just derive a new class that overrides the OnTabStopChanged method and automatically set it back to false again. Here is the impl...

  public class NonTabStopRadioButton : RadioButton
  {
      protected override void OnTabStopChanged(EventArgs e)
      {
          base.OnTabStopChanged(e);

          if (TabStop)
            TabStop = false;
      }
  }

Then always use the NonTabStopRadioButton in your application instead of the standard one.

Phil Wright
  • 22,580
  • 14
  • 83
  • 137
0

only one control can have input focus at the time i think, so when they click the radio button it will get focus.. But what if you do something like this?

rbFMV.GotFocus += (s, e) => someothercontrol.Focus();

also, have you looked at the TabStop property?

-edit-

i see you have, sorry, missed that :/

aL3891
  • 6,205
  • 3
  • 33
  • 37
  • That's what I had originally, and it works for tabbing—the control gets skipped when tabbing—but when the user **clicks** on the radio button, the focus jumps away, which is certainly not what I want. Grrr – Adam Rackis Jun 15 '11 at 21:09
  • Im not sure you can avoid it getting focus when its clicked, but perhaps you can set the focus to the other control by handling the Clicked event for the radiobutton? – aL3891 Jun 15 '11 at 21:13
  • But that's the thing, when the radio button is clicked, I want the radio button to be focused. Overriding the GotFocus to focus something else causes the RB to not be focused when it's clicked. Unless I misunderstood you. – Adam Rackis Jun 15 '11 at 21:15
  • ooh, so you want to be able to have focus but not be reachable by tabbing? then the TabStop property should do what you want – aL3891 Jun 15 '11 at 21:21
  • It should do that, but TabStop simply doesn't work for a RadioButton. Sorry, I should have specified that in my question—it's mentioned in my linked question – Adam Rackis Jun 15 '11 at 21:35