开发者

How to set variable in C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜