How do I make my picture box move using the arrow keys?

how to move an object with arrow keys in visual basic
how to move picture boxes in visual basic

I have created an animation using C# in Visual Studio, within a pictureBox using multiple images of pacman. I am now attempting to get the pictureBox to move around the form (up, down, right and left) by using the arrow keys but I cannot seem to get it working. Can anyone help me figure out why the pictureBox won't move? Thanks Here is my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Week9
{
public partial class Pacman : Form
{
    private Image[] pacmanImage = new Image[4];
    private int currentMouthPosition = 0;
    private int xPosition = 0;
    private int yPosition = 0;

    // The index of the current frame.
    private int FrameNum = 0;

    public Pacman()
    {
        InitializeComponent();
        this.KeyDown +=newSystem.Windows.Forms.KeyEventHandler(this.Pacman_KeyDown);
    }


    private void picFrame_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawImage(pacmanImage[currentMouthPosition], xPosition, yPosition, 32, 32);

    }

    // Load the images.
    private void Pacman_Load(object sender, EventArgs e)
    {
        pacmanImage[0] = Image.FromFile("..\\..\\pac32_left_close.png");
        pacmanImage[1] = Image.FromFile("..\\..\\pac32_left_open.png");
        pacmanImage[2] = Image.FromFile("..\\..\\pac32_left_wide.png");
        pacmanImage[3] = Image.FromFile("..\\..\\pac32_left_widest.png");
        // Display the first frame.
        picFrame.Image = pacmanImage[FrameNum];
    }

    // This is where I have tried to get the pictureBox to move
    private void Pacman_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Left:
                picFrame.Left -= 5;
                break;
            case Keys.Right:
                picFrame.Left += 5;
                break;
            case Keys.Up:
                picFrame.Top -= 5;
                break;
            case Keys.Down:
                picFrame.Top += 5;
                break;
        }
    }


    // Display the next image.
    private void tmrNextFrame_Tick(object sender, EventArgs e)
    {
        FrameNum = ++FrameNum % pacmanImage.Length;
        picFrame.Image = pacmanImage[FrameNum];
    }

    private void btnStartStop_Click_1(object sender, EventArgs e)
    {
        tmrNextFrame.Enabled = !tmrNextFrame.Enabled;
        if (tmrNextFrame.Enabled) btnStartStop.Text = "Stop";
        else btnStartStop.Text = "Start";
    }

  }
}
pictureBox1.Location = new Point(x, y);

you can write some codes like this in your form key press event or keydown or keyup events. get the current picture box location and add up or Reduce the position. you can use this code to get the current position :

pictureBox1.Location.X; //to get X
pictureBox1.Location.Y; // To get Y

VS 2008 moving a picture box with arrow keys-VBForums, I can't figure out how to make a picture box move when a key is use a picture box and displayed an image using a different type of control? Move A Picture Box With Arrow Keys? Oct 10, 2010. I have been trying to figure this out all night, so I figures it's time to ask you guys. I can't figure out how to make a picture box move when a key is pressed.

private void Pacman_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Left:
            mouthcurrentposition = 0;
            picFrame.yPosition -= 10;
            picFrame.Left(); //This should be a method that will draw your pacman
            break;
        case Keys.Right:
            mouthcurrentposition = 1;
            picFrame.yPosition += 10;
            picFrame.Right();
            break;
        case Keys.Up:
            mouthcurrentposition = 2;
            picFrame.yPosition -= 10;
            picFrame.Up();
            break;
        case Keys.Down:
            mouthcurrentposition = 3;
            picFrame.yPosition += 10;
            picFrame.Down();
            break;
    }
}

1. Moving An Object With WSAD Keys in VB 2012, To move the picture box up I have a button with the following code in: Pic1. This means that its not detecting my arrow keys. Is there any  Move object (image) with arrow keys using JavaScript function - In this example we will learn how to move object/image using arrow keys (left, top, right and down). We will call function in Body tag on "onkeydown" event, when you will down arrow keys object will be moved on the browser screen.

I know i'm super hella late but what you missed was this.KeyDown += Pacman_KeyDown on your main form

ARROW KEYS MOVEMENT IN VB, Moving a picture Box with arrow keys: My First Game. You can look through RodgerB's tutorial on making a Breakout Style Game to see how  I want to be able to move an image around the screen with JavaScript. The code I have below will put the image on the screen but will not let me move it around. Question: Want to be able to move the image around the screen with the arrow keys? I am certain there has to be a game loop that somehow is running all the time when the page is active.

Moving Picture Box with Arrow Keys, I'm trying to get a picture of an arrow to move up or down depending on the user input however it will not move. The arrow is there so the user  In this situation, the arrow keys move scroll bars along the right side and bottom of a drawing window. Resolution To resolve this issue, press SCROLL LOCK until your keyboard's Scroll Lock light (LED) is off.

Moving A Picture Box With Arrow Keys, Creating a new Windows Forms Application project, then create a new class with name CustomPictureBox and make it inherit the PictureBox  You’re working in a worksheet in Excel and you press one of the arrow keys on your keyboard to move to the next cell. But instead of moving to the next cell, the whole worksheet moved. Don’t panic.

I Have A Problem With Moving A Picture Box Using Arrow Keys., i was wondering how would you make a picture box or an object repond to the arrow keys. for example i have a picture and i want to move right  Hi @Moormanmar.. Are you attempting to move photos from your mobile device/tablet to Box or from a computer? If you are using a mobile device/tablet, you can use the Box.com app or m.box.com to upload files (If you are on iOS, you can also use Box Capture).

Comments
  • there are plenty of working examples on the internet.. have you tried a Google Search on the following C# moving PictureBox ? social.msdn.microsoft.com/Forums/windows/en-US/…
  • Yes I have looked at that example and many others and still can't seem to get it working
  • check this link out to..stackoverflow.com/questions/4779515/…
  • I have all the correct coding in place, I tried it on a simple picturebox in another project and it moved fine. I do not know what is stopping my picture box from moving in this case.
  • They have that in the form's constructor.