如何在C#中分配我的e.keyvalue? [英] How can I assign my e.keyvalue in C#?
问题描述
我试图通过在YT之后查看YT教程来学习编程太空入侵者; Visual Basic 2010 - 太空侵略者 - 第2部分 - 移动图片框键盘活动 - YouTube [ ^ ]
在教程中,它与Visual Basic一起使用,我尝试在c#中设置它。
在那个教程中,我试图让我的宇宙飞船(开始玩游戏时)左右移动。但是有一个错误
I was trying to learn to program space invaders by viewing a tutorial on YT following this; Visual Basic 2010 - Space Invaders - part 2 - moving the picturebox with keyboard events - YouTube[^]
In the tutorial it was used with Visual Basic and I tried to set it in c#.
In that tutorial I am trying to make my spaceship (when start playing the game) go left and right. But then there is an error
Severity Code Description Project File Line
Error CS0200 Property or indexer 'KeyEventArgs.KeyValue' cannot be assigned to -- it is read only Space Invaders C:\Users\******\Documents\Visual Studio 2015\Projects\Space Invaders\Space Invaders\Form1.cs 61
我可以对此提出一些建议吗?如果发生这种情况,我会很高兴的。 :)
我的尝试:
Please can I have some advise with this? I would be glad if that happens. :)
What I have tried:
namespace Space_Invaders
{
public partial class parentform : Form
{
#region variables
Boolean left;
Boolean rig;
#endregion
#region button
public parentform()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Hide();
pictureBox1.Hide();
pictureBox2.Hide();
button1.Hide();
button2.Hide();
panel1.Show();
Movecomp.Enabled = true;
Movecomp.Start();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
this.Close();
}
#endregion
#region timers
private void Movecomp_Tick(object sender, EventArgs e)
{
if (left == true)
{
pictureBox3.Left = -3;
}
}
#endregion
#region keypresses
private void movecomptleft(object sender, KeyEventArgs e)
{
if (e.KeyValue = Keys.A)
{
left = true;
}
}
private void movecomptstop(object sender, KeyEventArgs e)
{
}
#endregion
}
}
推荐答案
问题是:
The problem is:
if (e.KeyValue = Keys.A)
您想要检查相等性。为此,您必须使用两个 =
标志,而不是一个(一个 =
标志是为了分配)。此外, KeyValue
包含一个int,但你要根据 Keys
枚举的值来检查它,所以你必须更换带有KeyCode或KeyData的KeyValue(当您不关心修饰符时为KeyCode,当您关心修饰符时为KeyData)。
You want to check equality. For this, you have to use two =
signs, not one (one =
sign is for assignment). Additionally, KeyValue
contains an int, but you check it against a value of the Keys
enum, so you have to replace "KeyValue" with "KeyCode" or "KeyData" (KeyCode when you don't care about modifiers, KeyData when you do care about modifiers).
if (e.KeyCode == Keys.A)
这篇关于如何在C#中分配我的e.keyvalue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!