Winforms Timer for Dummies

前端 未结 4 1931
面向向阳花
面向向阳花 2020-12-03 03:09

I\'m trying to get a timer to tick once a second in a winform, when I look for advice on how to do this I find loads of stuff about threads. Well I don\'t care about threads

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 04:00

    You don't need threading if you use the winforms' timer.

    Drop a Timer control on your page, name it "timer" and paste the following code:

    public MainForm()
    {
        InitializeComponent();
    
        timer.Interval = 1000;
        timer.Start();
    
        timeLeft = 60;
        timer.Tick += Timer_Tick;
    
        // start things
        label1.Hide();
        button1.Hide();
        timer.Start();
    
    }
    
    public void Timer_Tick(object sender, EventArgs e)
    {
        timeLeft--;
    
        if (timeLeft <= 0) {
            timer.Stop();
            label1.Show();
            button1.Show();
        }
    }
    

    I suppose if you've come from a webby background, Events-driven programming is probably the thing you need to get start reading about if you want to understand how stuff on the desktops are programmed.

提交回复
热议问题