Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top