0

I've set KeyPreview = true; for my Form. I basically want to use the arrow keys to go to the next and previous images instead of changing focus to different controls. I've set the Handled property to true but still the focus changes on arrow key press.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete)
    {
        // Do stuff
    }
    else if (e.KeyCode == Keys.Left)
    {
        // Do stuff
        e.Handled = true;
    }
    else if (e.KeyCode == Keys.Right)
    {
        // Do stuff
        e.Handled = true;
    }
}

EDIT

The behavior I want to achieve is as follows.

Left Arrow Key -> Previous Image
Right Arrow Key -> Next Image

Now, I also have a few TextBoxes on my Form and I therefore do not want to go to next and previous images if those Textboxes are in focus because then it should navigate through the text instead.

Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
  • What do you need exactly? You have a form that has some controls on it and a picture box and you want to change current image that is displayed when pressing left/ right arrows ? – George Lica Jun 08 '16 at 09:39
  • `KeyDown`, `KeyPress` and `KeyUp` are entirely independent events. Most likely your other controls work with one of the other events, which you don't "interrupt". Both `KeyPress` and `KeyUp` are used extensively. – Luaan Jun 08 '16 at 09:41
  • @GeorgeLica Yes. I also have a few `TextBox`es on my `Form` and I therefore do not want to go to next and previous images if those `Textbox`es are in focus because then it should navigate through the text instead. – Abdul Fatir Jun 08 '16 at 09:42
  • @Luaan How should I interrupt them then? Or go about achieving the behavior I want to achieve? – Abdul Fatir Jun 08 '16 at 09:43
  • It still isn't entirely clear what behaviour you want. At one hand, you mention "avoiding changing the focus" when those keys are pressed (what kind of control changes focus on pressing the left key?). On the other, you mention "letting textboxes keep their behaviour". So you want to interrupt the events for some controls and not for others? – Luaan Jun 08 '16 at 09:45
  • @Luaan The focus moves through `Button`s and `TextBox`es when I press arrow keys. When the `TextBox` is in focus and user presses for example the right key, he obviously wants to move right in the text, not move to the next image, right? That's the behavior I am expecting. – Abdul Fatir Jun 08 '16 at 09:47
  • @AbdulFatir In what kind of control are you showing your images? – George Lica Jun 08 '16 at 09:48
  • @GeorgeLica In a `Panel`. – Abdul Fatir Jun 08 '16 at 09:48
  • @AbdulFatir So are you setting directly BackgroundImage property for your pannel, right? – George Lica Jun 08 '16 at 09:53
  • @GeorgeLica Yes, I am. – Abdul Fatir Jun 08 '16 at 09:56
  • Okay, play with the PreviewKeyDown event ... wire to this event for every control in your form and do your logic. – George Lica Jun 08 '16 at 10:00
  • @AbdulFatir maybe this helps: http://stackoverflow.com/questions/1318236/how-to-disable-navigation-on-winform-with-arrows-in-c – George Lica Jun 08 '16 at 10:03
  • @GeorgeLica That didn't help. I did it in a different way. Check my answer. – Abdul Fatir Jun 08 '16 at 10:09

1 Answers1

1

This worked for me.

  1. Do not set KeyPreview = true; for the Form.
  2. Override ProcessCmdKey and process as needed if any of the TextBoxes do not have focus.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    
        if (textBox1.ContainsFocus || textBox2.ContainsFocus || textBox3.ContainsFocus)
        {
            return base.ProcessCmdKey(ref msg, keyData);
        }
    
        if (keyData == Keys.Delete)
        {
            removeRect();
            return true;
        }
        else if (keyData == Keys.Left)
        {
            previousImg();
            return true;
        }
        else if (keyData == Keys.Right)
        {
            nextImg();
            return true;
        }
    
        else
        {
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
    
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58