我试图让标签显示一些文本,然后在一段时间后刷新自身并能够稍后重新显示其他内容。但目前我不知道如何使标签暂停(如果可能的话)。
到目前为止我的代码:
foreach (var x in mod)
{
labelWARNING.Visible = true;
labelWarningMessage.Text = "This module has a prerequisite module: " + x;
//need a pause here to give user sufficient time to read the above text
//labelWarningMessage.Text = "";
}
从您的问题来看,您似乎需要更改状态标签之类的值,以定期向用户显示信息。如果您使用 winforms,您可以使用计时器和委托,如下所示:
从您的问题来看,您似乎需要更改状态标签之类的值,以定期向用户显示信息。如果您使用 winforms,您可以使用计时器和委托,如下所示:
//First create a delegate to update the label value
public delegate void labelChanger(string s);
//create timer object
Timer t = new Timer();
//create a generic List to store messages. You could also use a Queue instead.
List<string> mod = new List<string>();
//index for list
int cIndex = 0;
//add this in your Form Load event or after InitializeComponent()
t.Tick += (timer_tick);
t.Interval = 5000;//how long you want it to stay.
t.Start();
//the timer_tick method
private void timer_tick(object s, EventArgs e)
{
labelWarningMessage.Invoke(new labelChanger(labelWork), mod[cIndex]);
cIndex++;
}
//the method to do the actual message display
private void labelWork(string s)
{
labelWARNING.Visible = true;
labelWarningMessage.Text = "This module has a prerequisite module: " + s;
}
我希望这有帮助。祝你好运。
EDIT: 我以为我很久以前就发布了这段代码,只是回来后发现我没有......但也许有人可能会发现它有用。
此外,在这种情况下,此方法将是多余的,因为单独创建 Timer 将在不使用委托的情况下工作,并且仅对委托使用和 UI 线程访问部分有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)