문제

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;
        }
도움이 되었습니까?

해결책

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());    
}

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top