我有一个简单的问题,但我无法在任何地方找到直接答案
我有一个C#程序,在用户按下" start"按钮。所以1,2,3等等,但增量是在偶然的不同持续时间内进行的,即
1 - > [4秒后] 2 - > [7秒后] 3 - >等
并每毫秒检查一次程序
我想在GUI上添加一个指示,让用户知道到达的数量
我正在考虑使用一个标签来获取它" Counter:"
// CounterLabel
//
this.CounterLabel.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.CounterLabel.AutoSize = true;
this.CounterLabel.Location = new System.Drawing.Point(1090, 35);
this.CounterLabel.Name = "CounterLabel";
this.CounterLabel.Size = new System.Drawing.Size(58, 17);
this.CounterLabel.TabIndex = 52;
this.CounterLabel.Text = "Counter:";
然后我有两个问题:
1)我是否需要一个只读文本框来托管更改的号码
//
// CounterValue
//
this.CounterValue.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.CounterValue.BackColor = System.Drawing.SystemColors.Control;
this.CounterValue.Location = new System.Drawing.Point(1149, 32);
this.CounterValue.Name = "CounterValue";
this.CounterValue.Size = new System.Drawing.Size(84, 22);
this.CounterValue.TabIndex = 53;
this.CounterValue.ReadOnly = true;
//this.CounterValue.Text += this.GetCounterValue();
或者有办法让它只使用标签?
2)如何执行控制以查看是否必须更新UI?我的意思是,每隔msec检查要显示的值,我希望每毫秒更新一次接口[不使用"更新"按钮要求显示达到的值]
提前感谢那些试图提供帮助的人
答案 0 :(得分:2)
1)是的,您需要标签旁边的只读文本框。
2)在表格中添加一个方法如下:
void UpdateCounter()
{
if (InvokeRequired)
{
BeginInvoke(new MethodInvoker(UpdateCounter));
return;
}
CounterValue.Text = Counter.ToString();
}
3)每次更改计数器时都调用此方法。
或者您可以使用计时器来调用UpdateCounter函数。
答案 1 :(得分:1)
另一种选择是使用定时器控制它会自动更改值
timer1.star();
private void timer1_Tick(object sender, EventArgs e)
{
//Your code
}