如何使标签每秒增加1

时间:2014-04-26 11:49:37

标签: c# winforms

例如,我希望标签每秒增加1点。

例如

标签将等于1,然后在一秒钟后它将等于2,然后在一秒钟后它将等于3,一秒后它将等于4等。

就像生存评分机制一样。看看你能坚持多久而不会死亡。

2 个答案:

答案 0 :(得分:1)

从工具栏中添加timer到您的表单,将其设置为interval1000(= 1s)并将Enabled设置为true,写一个Tick事件处理程序如下:

var num = int.parse(lbl_increase.Text);
lbl_increase.Text = (num+1).ToString();

答案 1 :(得分:1)

你可以这样做

1)将计时器拖到表单中。 计时器位于组件选项卡下(见图片)

here is the timer

2)点击您的计时器(它在您的表单上不可见,但 表单下方)并转到属性标签

3)更改'已启用'真实并改变间隔'到1000。

4)转到事件标签,为“' Tick'

创建一个事件

5)在您的方法中添加此代码:

private void timer1_Tick(object sender, EventArgs e)
        {
            //Convert the text from your label to an int
            int i = Convert.ToInt32(yourLabel.Text);

            //increment the int
            i++;

            //set the text to the value of the int
            yourLabel.Text = i.ToString();
        } 

我希望它有所帮助