【发布时间】:2013-11-29 06:13:15
【问题描述】:
我正在尝试通过静态方法设置toolStripStatusLabel:
public static void loggedChanged()
{
if (SM_Class.logged)
toolStripStatusLabel1.Text = "Conectado: " + SM_Class.logged_user.username;
else
{
toolStripStatusLabel1.Text = "Desconectado";
}
}
这来自于类中的另一个静态声明
public static Boolean logged
{
get { return _logged; }
set
{
if (_logged != value)
{
_logged = value;
Main.loggedChanged();
}
}
}
我得到错误:
非静态字段、方法或属性“WindowsFormsApplication1.Main.toolStripStatusLabel1”需要对象引用
我应该改变什么才能更新toolstriplabel?提前致谢。
【问题讨论】:
-
您的
toolStripStatusLabel1也应该是静态的。 -
谢谢。但是我应该在哪里将 toolstripStatusLabel1 声明为静态?现在声明如下: private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
-
静态初始化在一些实例字段之前运行。这就是为什么您可能会清除此异常。因此,请确保 toolStripStatusLabel1 在使用前已初始化
-
如果我添加此声明,我不会收到错误:ToolStripStatusLabel toolStripStatusLabel1 = new ToolStripStatusLabel(); - 但不显示工具条状态标签。