Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable navigation on WinForm with arrows in C#?

Tags:

c#

focus

winforms

I need to disable changing focus with arrows on form. Is there an easy way how to do it?

Thank you

like image 333
Martin Vseticka Avatar asked Aug 23 '09 10:08

Martin Vseticka


People also ask

How do I hide winform?

If you Don't want the user to be able to see the app at all set this: this. ShowInTaskbar = false; Then they won't be able to see the form in the task bar and it will be invisible.


2 Answers

Something along the lines of:

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (Control control in this.Controls)
        {
            control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
        }
    }

    void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
        {
            e.IsInputKey = true;
        }
    }
like image 180
andynormancx Avatar answered Sep 27 '22 23:09

andynormancx


I tried this aproach, where the form handles the preview event once. It generates less code than the other options.

Just add this method to the PreviewKeyDown event of your form, and set the KeyPreview property to true.

private void form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Up:
        case Keys.Down:
        case Keys.Left:
        case Keys.Right:
            e.IsInputKey = true;
            break;
        default:
            break;
    }
}
like image 30
carlososuna86 Avatar answered Sep 27 '22 23:09

carlososuna86