C# tab order with radio buttons

谁说我不能喝 提交于 2019-12-22 11:42:42

问题


SO i have a windows form application, there is a textbox which tells you what grocery item you want to select. There are 3 radio buttons answers which you can chose from and a button that submits that answer. What i want is to be able to navigate through those radio buttons using tab . I have tried the tab order thing but it doesnt work. Any suggestions?


回答1:


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.



来源:https://stackoverflow.com/questions/12758038/c-sharp-tab-order-with-radio-buttons

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