在不单击的情况下显示标签消息
本文关键字:显示 标签 消息 情况下 单击 | 更新日期: 2023-09-27 18:33:57
所以我希望能够在不单击标签的情况下在标签上显示消息。
public void servicestatus_Click_1(object sender, EventArgs e)
{
var mavbridgeservice = new System.ServiceProcess.ServiceController("MavBridge");
if (mavbridgeservice.Status == ServiceControllerStatus.Running)
{
servicestatus.Text = ("The service is running!");
}
else
{
servicestatus.Text = "The service is stopped!";
}
}
我将 (null, null) 绑定在 (对象发送器, EventArgs e) 中,但这给出了错误,我不知道为什么。我里面的代码与任何点击没有任何关联。
您显然希望显示服务器的状态。
但是,当用户要求更新时,您不希望更改它,您希望它自动更改,对吗?
您有两种选择。编辑:我现在看到选项#1不起作用,您将需要下面的计时器选项。 我删除了选项 #1
选项 #2如果它在程序之外更改,则可以添加一个计时器,该计时器每隔一秒或两秒询问一次,具体取决于您希望更新用户的速度,并在这段时间内添加代码以设置标签
public Timer t = new Timer();
然后在你的主窗体构造函数中,在 InitializeComponent(); 行下添加这个
t.Interval = 1000;
t.Tick+=new EventHandler(t_Tick);
t.Enable=true;
在计时器中。Tick 事件运行您的代码以确定状态(标签的点击事件中当前的内容)
void t_Tick(object sender, EventArgs e)
{
var mavbridgeservice = new System.ServiceProcess.ServiceController("MavBridge");
if (mavbridgeservice.Status == ServiceControllerStatus.Running)
{
servicestatus.Text = ("The service is running!");
}
else
{
servicestatus.Text = "The service is stopped!";
}
}
在
事件处理程序的参数中放置 null null 会给你一个错误,因为代码需要知道发送此请求的内容(对象,在本例中为标签)和它使用的事件参数(e) 你不能为空这些,它需要它们才能运行。 这就是为什么它给出错误。
同样如前所述,当您单击时,这可能会触发,因为您尚未将标签单击事件与此代码链接(click_1显示有一个传统的单击,它链接到您不使用的点击)