4

I have a WinForm with 3 group boxes, one with combo boxes, and two with radio buttons. I set all of them and their children controls to "TabStop = false", but when I cycle with TAB, the currently selected radio button in each of the last two group boxes gets focused.

If there's no way to change this behavior, what would be a good event to catch and move the focus away? I can't find an "OnFocus" event.


The solution is to set one method (code below) to handle the "Enter" event of every radio button in the form (if that's what you wish).

Actually, I only did it for the radio buttons of the first group box and it worked, the second group box's radio buttons don't get focus, even though their "Enter" events are not handled. This is not the behavior you would have expected.

private void radiobuttonXGroup1_Enter(object sender, EventArgs e)
{
   SomeOtherControl.Focus();
}

In the *.Designer.cs file you edit every Enter event (for each radio button) to point to one event handler (the above method).

this.radiobutton1Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton2Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton3Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
OIO
  • 363
  • 7
  • 16
  • 1
    @Robaticus, question starts with "I have a WinForm...]" – Kashif Aug 24 '10 at 19:18
  • I edited the question in response to Robaticus question. The problem is that my browser is no longer working with StackOverflow site. I can only edit already posted questions, but can't post new questions or add comments (using Firefox now). – OIO Aug 24 '10 at 19:25
  • 1
    You are probably being throttled. That happens when you never accept an answer :) – Hans Passant Aug 24 '10 at 19:29
  • @Muhammid: Actually, when I put in my comment, the text "a winform with" was not there. It was subsequently edited to include that text. Look at the version history. – Robaticus Aug 24 '10 at 19:51
  • @Hans: In my comment to Muhammad I mentioned I was using Firefox to be able to add a comment, so how do you go from a browser issue to a "throttled" issue? Lets not get sidetracked. – OIO Aug 24 '10 at 20:05

2 Answers2

7

Setting the TabStop to False on a RadioButton to prevent tabbing to the control works until you actully select the radio button without any additional overrides like suggested by @msergeant.

EDIT

The following code prevents the code from getting a tab key event:

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

Radio buttons behave differently with respect to Tab from other controls in that they work in sets or groups based on setting the tab index or placing then radio buttons in a group box.

Zamboni
  • 7,897
  • 5
  • 43
  • 52
  • 1
    Thanks for the info. I still think it is not an expected behavior, expected behavior would have been for radio buttons not to get focus on a TAB key press if set to TabStop = false (just like the other controls do). – OIO Aug 25 '10 at 21:32
  • Thanks your comment made me think some more about your question and my answer. – Zamboni Aug 26 '10 at 02:52
  • Thanks for taking the time. I compared the two events and the "Enter" event seems to be the right one: "Occurs when the control becomes the active control of the form." vs "Occurs whenever the 'checked' property changes value." When cycling through the controls with the TAB key, the radio buttons' "checked' property doesn't change, they only get focus. – OIO Aug 26 '10 at 03:24
  • Wow... I've been tearing my hair out for an hour now, and this resolved my issue beautifully! – Taegost Jun 02 '14 at 19:18
5

The MSDN documentation for RadioButton.TabStop states "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code". Which basically means, "This isn't going to work how you expect it to".

With that said, the Enter event will fire when the button receives the focus. You can try to use that to move focus to another control.

msergeant
  • 4,771
  • 3
  • 25
  • 26
  • Yes, saw that a few minutes ago, I was hoping the TabStop would work, it looks more elegant, and is only the radio buttons that don't work, the other controls do. – OIO Aug 25 '10 at 21:22