Question

In Windows Forms program (VS13) I've added a timer, but it's updates only when I'm pressing on it. How to make it update value without pressing?

private void label17_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            this.label17.Text = d.Hour + ":" + d.Minute;
        }
Was it helpful?

Solution

You can use the following code:

private void Window_Loaded(object sender, RoutedEventArgs e)
{    
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    this.label17.Text=(DateTime.Now.Hour.ToString() + ":" +
    DateTime.Now.Second.ToString());    
}

OTHER TIPS

That's not a timer. You've added that to the label's click event handler, so of course it only updates when you click on it. You need to implement a Timer object and update the label on its tick event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top