C# tab order with radio buttons

我只是一个虾纸丫 提交于 2019-12-06 09:37:00

Windws Forms lets you only Tab into the group. One way to hack around it is to get all Buttons in seperate Groups by putting group boxes around each one of them.

Although this allows you to tab through them, they are now disjointed and will not automatically deselect. To do so register for the event that fires on selection and deselect the others programatically.

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

    }
}

I tested this approach and it seems to do the job.

Forms is not the modern way of doing things. Consider moving to WPF for new Projects.

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