Help to deal with the timer. It is necessary that it shows minutes and seconds, as well as a countdown.

I can't add minutes. Time is stupid and that's all. Type field 00:60, the first minute should appear, and seconds to go from the beginning (01:00), but I just have seconds to go (60, 61, 62, etc.), how can I fix this?

public int sec, min; private void Timer_Tick(object sender, object e) { if (sec == 60) { sec = 60; min += 1; } if (min == 60) min = 1; } private void progressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { textBlock1.Text = ProgressPos.Value.ToString(); textBlock2.Text = ProgressPos.Value.ToString(":"+min + "" + sec); } 
  • 2
    if (sec == 60) sec = 60; what is this for? - qzavyer
  • Show the code where you start the timer - Gardes
  • timer = new DispatcherTimer() { Interval = new TimeSpan() }; timer.Tick += Timer_Tick; timer.Start(); - Cocaine Snow

1 answer 1

Do not keep separate minutes and seconds. Do you have the value of seconds? Get a TimeSpan from it and output it in the required format:

 var time=TimeSpan.FromSeconds(sec).ToString("HH:mm:ss"); 
  • Damn, I do not even know. I did something, but it's not right, some kind of crap. Stupid again seconds go and everything (well, apparently because I wrote crap). private void progressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { TimeSpan time = TimeSpan.FromSeconds(0); textBlock1.Text = ProgressPos.Value.ToString(+time+""); } private void progressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { TimeSpan time = TimeSpan.FromSeconds(0); textBlock1.Text = ProgressPos.Value.ToString(+time+""); } - Cocaine Snow
  • ProgressPos.Value is what? Why is TimeSpan.FromSeconds(0) taken? - Zufir
  • ProgressPos is the x: Name of the ProgressBar (assigned to XAML). TimeSpan.FromSeconds(0) this is for the countdown to start from 0th howl second (well, I found it in the intent yesterday) - Cocaine Snow
  • TimeSpan.FromSeconds(0).ToString() will return "00:00:00" . You use this line as a format for your seconds. Those. The 132nd second will 00:01:32 as 00:01:32 , because you do not form the time from seconds, but still output the number of these seconds. Write simply: textBlock1.Text=TimeSpan.FromSeconds(ProgressPos.Value,"HH:mm:ss"); - Zufir
  • Well, if you write as you say textBlock1.Text=TimeSpan.FromSeconds(ProgressPos.Value,"HH:mm:ss"); then I get an error that Ни одна из перегрузок метода "FromSeconds" не принимает 2 аргументов. ArgumentException and OverflowException - Cocaine Snow