Do wpf forms have timer control?

Do wpf forms have timer control?

+2


source to share


2 answers


Try System.Windows.Threading.DispatcherTimer .



+5


source


WPF does not have timer controls per se, but can be implemented quite easily:



using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

public partial class Timer : Label
{
    DispatcherTimer DTimer;
    int Seconds = 0, Minutes = 0;

    public Timer()
    {
        DTimer = new DispatcherTimer();
        DTimer.Interval = TimeSpan.FromSeconds(1);
        DTimer.Tick += new EventHandler(Tick);

        Content = "00:00";

        HorizontalContentAlignment = HorizontalAlignment.Center;
        VerticalContentAlignment = VerticalAlignment.Center;
    }

    void Tick(object sender, EventArgs e)
    {
        Seconds++;

        if (Seconds == 60)
        {
            Seconds = 0;
            Minutes++;
        }

        Content = string.Format("{0:D2}:{1:D2}", Minutes, Seconds);
    }

    public void Start() { DTimer.Start(); }

    public void Stop() { DTimer.Stop(); }

    public void Reset()
    {
        DTimer.Stop();

        Seconds = Minutes = 0;

        Content = "00:00";
    }
}

      

0


source







All Articles