如何在不选择的情况下浏览 Windows 窗体中的 RadioButton 组 [英] How to tab through RadioButton group in Windows Forms without selecting

查看:82
本文介绍了如何在不选择的情况下浏览 Windows 窗体中的 RadioButton 组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一堆控件的 Windows 窗体 GUI,其中一个是包含一组 RadioButton 的 FlowLayoutPanel.

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

我可以使用 Tab 键切换到第一个 RadioButton(TabIndex 和 TabStop 设置正确),但是当我再次点击 Tab 键时,它会将我带到表单上的下一个控件,而不是 FlowLayoutPanel 中的下一个单选按钮.

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.

一旦第一个 RadioButton 获得焦点,通过该组的唯一方法是使用向上/向下箭头,但这实际上是选择 RadioButton.选择 RadioButton 会启动一些其他的东西,所以我只想找到一种方法来浏览列表以找出要选择的内容.

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.

理想情况下,我想要的是让每个 RadioButton 像页面上的任何其他控件一样运行,并允许我在不选择 RadioButton 的情况下浏览整个组,直到我点击 Space.

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.

推荐答案

此 Tab 键顺序是默认的 Windows 行为,很少有好的做法进行更改.

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

这是因为组中的单选按钮让 TabStop 以这种方式工作.

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

要更改此设置,请在每次重置时将 Tab Stop 显式设置为 true:

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;
    }
}

这篇关于如何在不选择的情况下浏览 Windows 窗体中的 RadioButton 组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆