我正试图这样做,当我按下向上箭头时,它会向上移动图片框,向下箭头向下移动,等等。但我似乎无法让它发挥作用。它给了我错误:
无法修改返回值 'System.Windows.Forms.Control.Location'因为它不是变量
这是我的代码:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up)
{
ImgGuy.Location.Y--;
}
else if (e.KeyCode == Keys.Down)
{
ImgGuy.Location.Y++;
}
else if (e.KeyCode == Keys.Left)
{
ImgGuy.Location.X--;
}
else if (e.KeyCode == Keys.Right)
{
ImgGuy.Location.X++;
}
非常感谢任何帮助。
答案 0 :(得分:1)
您必须重新创建新的Location
:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Point l;
if(e.KeyCode == Keys.Up)
{
l = new Point(ImgGuy.Location.X, ImgGuy.Location.Y - 1);
}
else if (e.KeyCode == Keys.Down)
{
l = new Point(ImgGuy.Location.X, ImgGuy.Location.Y + 1);
}
else if (e.KeyCode == Keys.Left)
{
l = new Point(ImgGuy.Location.X - 1, ImgGuy.Location.Y);
}
else if (e.KeyCode == Keys.Right)
{
l = new Point(ImgGuy.Location.X + 1, ImgGuy.Location.Y);
}
ImgGuy.Location = l;
}
答案 1 :(得分:0)
试试这个:
ImgGuy.Location = new Point(ImgGuy.Location.X+1, ImgGuy.Location.Y+1) // etc
问题是Location
会返回该位置的副本。
或者,请设置Control.Left
和Control.Top
。
答案 2 :(得分:0)
你需要产生一个新的点
在这种情况下,X增加,又向左移动
Pic.Location = new Point(Pic.Location.X + 1, Pic.Location.Y);