Every things is ok form1+picturebox1 i can use good below codes:
public Form1()
{
InitializeComponent();
KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right) x += 5;
else if (e.KeyCode == Keys.Left) x -= 5;
else if (e.KeyCode == Keys.Up) y -= 5;
else if (e.KeyCode == Keys.Down) y += 5;
pictureBox1.Location = new Point(x, y);
}
BUT; if i add listbox to write position picturebox, i can not move picturebox with arrow keys how can i do that?
You need to Invalidate which will redraw your UI; In order to see those changes that you made.
You can choose to redraw certain regions with the overload
Invalidate(Region region)
or you can invalidate the whole control by doingControl.Invalidate()
.Try addign:
this.Invalidate();
after you've created the newPoint
.You can also use
Refresh()
which will invalidate and force the control to redraw.Here is an MSDN Article about "How to: Handle Keyboard input at Form Level" you might want to read that and compare it to your current code. You might want to change KeyDown to KeyPress instead. And also as I said in my comment, it really should work without forcing the form to redraw using
Invalidate()
orRefresh()
.Probalby your listbox gets the focus an not the form, you probably have to listen the listboxs keydown-event.
EDIT:
Use this focusable PictureBoxEx and listen to its previewkeydown-event :
EDIT:
Or use the following. with this you don't have to move a control in the form, but just move the image within the control.