How to set variable in C# [closed]
How to make this works? :
private string KeyChar = "";
public void SetMyKey(object sender, KeyEventArgs e) {
KeyChar = e.KeyCode; //ERROR
}
public void MyKeyDown(object sender, KeyEventArgs e) {
开发者_开发知识库 if (e.KeyCode = Keys.KeyChar) { //ERROR
Function();
}
}
if (e.KeyCode == Keys.Enter) { //Thanks Reed!
You are assigning instead of comparing
http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx
In C#, operator =
means assignment, and operator ==
means comparison:
if (a == b)
{
a = 1;
b = x;
}
Instead of using a method, I would use a property - you'll also want to use the correct type here (Keys, instead of string). In addition, you need to use ==
to check equality, since =
is for assignment in C#:
public Keys KeyChar { get; set; }
public void MyKeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == this.KeyChar) {
Function();
}
}
精彩评论