带有单选按钮的C#选项卡顺序

时间:2012-10-06 08:16:43

标签: c# tab-ordering

我有一个Windows窗体应用程序,有一个文本框,告诉您要选择的杂货项目。您可以选择3个单选按钮答案,并提供一个提交该答案的按钮。我想要的是能够使用选项卡浏览这些单选按钮。我已经尝试过Tab键顺序,但它不起作用。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

Windws Forms允许您只将Tab键添加到组中。破解它的一种方法是通过在每个按钮周围放置组框来获取所有按钮在单独的组中。

虽然这允许您通过它们进行制表,但它们现在已脱节,并且不会自动取消选择。为此,请注册选择时触发的事件,并以编程方式取消选择其他事件。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private List<RadioButton> allMyButtons;

        public Form1()
        {
            InitializeComponent();
            allMyButtons = new List<RadioButton>
            {
                radioButton1,
                radioButton2
            };
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton sendingRadio = (sender as RadioButton);
            if(sendingRadio == null) return;
            if(sendingRadio.Checked == true){
                foreach(var rb in (from b in allMyButtons where b != sendingRadio select b))
                {
                    rb.Checked = false;
                }
            }
        }

    }
}

我测试了这种方法,它似乎完成了这项工作。

表格不是现代的做事方式。考虑转移到WPF以获取新项目。