Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForm - TabStop Not Working

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);
like image 777
OIO Avatar asked Dec 16 '22 22:12

OIO


2 Answers

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.

like image 83
Zamboni Avatar answered Dec 30 '22 14:12

Zamboni


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.

like image 43
msergeant Avatar answered Dec 30 '22 14:12

msergeant