How to tab through RadioButton group in Windows Forms without selecting

本小妞迷上赌 提交于 2019-12-11 04:47:34

问题


I have a Windows Forms GUI that has a bunch of controls, one of which is a FlowLayoutPanel containing a group of RadioButtons.

I can tab to the first RadioButton (TabIndex and TabStop set properly), but when I hit tab again it takes me to the next control on the form, not the next radio button in the FlowLayoutPanel.

Once the first RadioButton has focus, the only way to go through the group is to use up/down arrows, but that actually selects the RadioButton. Selecting a RadioButton kicks off some other stuff, so I just want a way to tab through the list to figure out what to select.

What I want, ideally, is to have each RadioButton act like any other control on the page and allow me to tab through the whole group without selecting a RadioButton until I hit Space.


回答1:


This tab order is default Windows behaviour and it is rarely good practice to change this.

It happens as the Radio Buttons in a group have TabStop juggled to work this way.

To change this, explicitly set the Tab Stop to true every time it is reset:

public Form1()
{
    InitializeComponent();

    foreach (var control in this.flowLayoutPanel1.Controls.OfType<RadioButton>())
    {
        control.GotFocus += (s,a) => SetTabStops(flowLayoutPanel1);
        control.CheckedChanged+= (s,a) => SetTabStops(flowLayoutPanel1);
    }
}

private static void SetTabStops(Control host)
{
    foreach (Control control in host.Controls)
    {
        control.TabStop = true;
    }
}


来源:https://stackoverflow.com/questions/22689430/how-to-tab-through-radiobutton-group-in-windows-forms-without-selecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!