I am writing a program where you need to use the "left" and "right" keys. You need to choose the correct answer and toggle the key "left" or "right" to go to another question. Everything works, only there is one problem, when you press a key from the keyboard, it first switches the value of radioButton ', and only then goes to another question.

Is it possible to set some property for radioButton 'so that it does not respond to the keyboard?

    3 answers 3

    try to set keydaun events with all radiobuttons for this method (no need to duplicate it - all with the same method)

     private void radioButton1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == System.Windows.Forms.Keys.Left || e.KeyCode == System.Windows.Forms.Keys.Right || e.KeyCode == System.Windows.Forms.Keys.Up || e.KeyCode == System.Windows.Forms.Keys.Down) e.SuppressKeyPress = true; } 

    As long as the element focused on such a handler is in focus, the keys are simply ignored. Totally. No other handlers are called.

      This is not done in WinForms. At least on the go. The maximum of what can be achieved without "dancing with a tambourine" - disabling the transition to the Tab key using the TabStop property. But, it does not act on the arrow keys.

      I advise you to look in the direction of WPF. There are more opportunities.

      • and no dancing with a tambourine, virtual methods are needed for that - rdorn
      • If the form / container is used only for navigation, and if not? My IMHO, the question itself is put in such a way that several correct answers are possible depending on the specific situation. - Streletz

      If the arrow keys have a single purpose on a form (or control container), then you can intercept and process them at the form level, without sending events from these keys to nested controls. This is done quite simply, it is enough for the form to override the key handling method:

       protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.Left: //код переключения на предыдущий вопрос return true; case Keys.Right: //код переключения на следующий вопрос return true; default: //для всех остальных клавиш оставляем базовую обработку return base.ProcessCmdKey(ref msg, keyData); } } 

      There is another alternative solution using MenuStrip , it is described in this answer . If your program uses the menu, then this option may be interesting.