2017-07-27 55 views
-1

这是我第一次在一个文本框错误说(在infoGather文本)“的对象引用需要非静态字段...”

private static void infoGather_keydown(object sender,KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
     if (username == null) 
     { 
      username = infoGather.Text; 
     } 
     else 
     { 
      password = infoGather.Text; 
      if (username == "admin" && password == "admin") 
      { 
       MessageBox.Show("welcome"); 
      } 
     } 
    } 
} 

遇到这个错误这是我第一次遇到它在TextBox

+0

在哪里定义了用户名,密码和infoGather?他们是成员变量,不是静态的权利? –

+0

是的定义。 –

回答

3

您的文本框infoGatheran是该类的一个实例的成员,而不是static。因此,您无法通过静态函数访问它。

如果你考虑一下它没有任何意义 - 静态方法属于类而不是特定实例,因此当你试图访问实例的成员时 - 它会是哪个实例?

要解决具体的错误 - 是否有任何真正的原因有这种方法static?如果不是从签名中删除static

相关问题