Search code examples
c#winformsrefresh

C# Form Refresh Confusion


Please take a look at this Stopwatch.cs file.

There is no Form Refresh function call in it. However, the two Label controls which display the elapsed times are refreshed every second.

Yet, when the "resetButton_Click()" is called, the display of the elapsed times are not refreshed.

  • Why the different behavior?
  • How to fix the problem and have the display refreshed when the resetButton is clicked? (I tried the solution from here, but that didn't work)

Solution

  • You're only setting the value of the variables in the resetButton_Click() method but you don't update the values to the Labels themselves. Add this to the end of the resetButton_Click method and it will work as expected:

    _totalElapsedTimeDisplay.Text = _totalElapsedTime.ToString();
    _currentElapsedTimeDisplay.Text = _currentElapsedTime.ToString();
    

    Also, there's no need for any Form Refresh method to be called if you set the Text property of the Label control. Winforms takes care of "refreshing" the controls to show the new values without your interference, so the first case works as expected.