0

I'm trying to make a form move left or right denoted by a primary keystroke by a number of pixels denoted by a secondary keystroke but so far I've not been able to do anything. As far as I can tell I should have no problem here. I have set KeyPreview to true as well.

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        this.Close();
        int movement = 0;
        if (e.KeyCode == Keys.R)
            lastEntered = 'r';
        else if (e.KeyCode == Keys.L)
            lastEntered = 'l';
        else
        {
            if (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
            {
                movement = (int)(e.KeyCode - Keys.NumPad0);
                Console.WriteLine(movement);
                this.Left += movement;
            }
            if (lastEntered == 'r')
                this.Left += movement;
            else if (lastEntered == 'l')
                this.Left -= movement;
        }
    }
Roadblock
  • 1
  • 1
  • 1
    there are a few things wrong in your code. Why do you first of all close your form right away on any keydown? How are you expecting the form to move when its already closed? – nawfal Oct 27 '12 at 04:44
  • that was added to see if anything at all happened in the event of any keystroke. the idea being that I can clearly see if the form has closed, as it stands my code does nothing and I want to have at least something happen. – Roadblock Oct 27 '12 at 16:32
  • In that case you should better use a messagebox, since Close may not work in all cases. – nawfal Oct 27 '12 at 17:14

1 Answers1

1

There are a few things you should change. Take off this.Close from the keydown event first of all. Rest will be clear from the code.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.R)
        lastEntered = 'r'; //move to an enum preferably 
    else if (e.KeyCode == Keys.L)
        lastEntered = 'l'; //move to an enum preferably 
    else
    {
        if (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
        {
            int movement = (int)(e.KeyCode - Keys.NumPad0);

            if (lastEntered == 'r')
                this.Left += movement;
            else if (lastEntered == 'l')
                this.Left -= movement;
        }
    }
}

This is when I assume that the last pressed R or L should be remembered always and any other key down doesn't overwrite the lastEntered key which I believe is not what you would be looking into. Modify your code like this to make sense:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.R)
        lastEntered = 'r'; //move to an enum preferably 
    else if (e.KeyCode == Keys.L)
        lastEntered = 'l'; //move to an enum preferably 
    else
    {
        lastEntered = 'u'; //u for unwanted - only if even num keys should overwrite
        if (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
        {
            int movement = (int)(e.KeyCode - Keys.NumPad0);

            if (lastEntered == 'r')
                this.Left += movement;
            else if (lastEntered == 'l')
                this.Left -= movement;
        }
        //else                   //so that num keys dont overwrite and hence you
        //    lastEntered = 'u'; //could keep pressing num keys to move the form
    }
}
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • So after a bit of work and a lot of swearing I found out my problem, I needed to add: `KeyDown+=Form1_KeyDown(...)` to the InitializeComponenent() method. – Roadblock Oct 28 '12 at 00:40
  • @Roadblock yes indeed. That's how you should register every event. Its easier if you can do it from the designer's property section, where visual studio will automatically do it for you. – nawfal Oct 28 '12 at 05:11