views:

80

answers:

2

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);
+2  A: 

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
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
Thanks for pointing out the right event for the job.
OIO
+1  A: 

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
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
Thanks your comment made me think some more about your question and my answer.
Zamboni
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