I'm trying to make a picture move whenever I press "a". I'm using this function:
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == 65)
{
peanut.Location = new System.Drawing.Point(0, 0);
}
}
Above the function it says 0 references if that helps. Also "peanut" is the name of the picture box.
When using peanut.Location = new System.Drawing.Point(0, 0); when the form loads, it works. I think the problem has to do with getting the key input, not how I'm moving the picture.
I tried using the KeyPress function, but for some reason it isn't working. This might be because e.KeyChar 65 isn't A, but if it isn't, could somebody show me a list of all the values and keys associated with them?
CodePudding user response:
Try setting the Form's KeyPreview
to true
in the Form's load
event, like this:
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
}
Also if you want to move the picture box when the letter, a
which has the value, 97
is pressed, try this:
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == 97)
{
peanut.Location = new System.Drawing.Point(0, 0);
}
}