question

frendlyNub-3829 avatar image
0 Votes"
frendlyNub-3829 asked WayneAKing-0228 answered

Reverse number in a string to count down

I have display timer that resets after a four seconds and on display i wanna it count down instead of up. Here is my code:

 private void TimerTick1()
         {
             string time2;
             TimeSpan timeElapsed = DateTime.Now - startTime;
             time = timeElapsed.TotalSeconds.ToString("0.0");
             time2 = timeElapsed.TotalSeconds.ToString("0.0");
             time2 = Reverses(time2);
             label1.Text = time2;
             Console.WriteLine(time2);
             secs = float.Parse(time);
             if (secs >= 4.000f)
             {
                 seq = seq + 1; // image sequence
                 startTime = DateTime.Now;
             }
    
             //====================image==============================
             if (clas == 1)
             {
                 if (seq == 3)
                 {
                     seq = 0;
                 }
                 if (seq == 0)
                 {
                     pictureBox1.Image = CoEtimer.Properties.Resources.convice;
                 }
                 if (seq == 1)
                 {
                     pictureBox1.Image = CoEtimer.Properties.Resources.convphys;
                 }
                 if (seq == 2)
                 {
                     pictureBox1.Image = CoEtimer.Properties.Resources.convgreen;
                 }
             } else if (clas == 2)
             {
                 if (seq == 4)
                 {
                     seq = 0;
                 }
                 if (seq == 0)
                 {
                     pictureBox1.Image = CoEtimer.Properties.Resources.convarca;
                 }
                 if (seq == 1)
                 {
                     pictureBox1.Image = CoEtimer.Properties.Resources.convice;
                 }
                 if (seq == 2)
                 {
                     pictureBox1.Image = CoEtimer.Properties.Resources.convfire;
                 }
                 if (seq == 3)
                 {
                     pictureBox1.Image = CoEtimer.Properties.Resources.convlight;
                 }
             }
         }
    
 public static string Reverses(string s)
         {
             char[] chararray = s.ToCharArray();
             Array.Reverse(chararray);
             string Txt = string.Empty;
             for (int i = 0; i <= chararray.Length - 1; i++)
             {
                 Txt += chararray.GetValue(i);
             }
             Console.WriteLine(Txt);
             return Txt;
         }

but it just replaces number's position. Any way to do that?

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

I have display timer that resets after a four seconds
and on display i wanna it count down instead of up.

It's rather difficult to tell what you are trying to
accomplish, especially with the string reversal.

Taking your general description of the objective at
face value - how to display the time elapsed as a
countdown rather than an incrementing count - one
simple approach (given that you know the limit) is
to subtract the elapsed time from the time limit
which is 4 seconds in your case.

For example:

 int duration = 4;
 for (int n = 0; n < duration; ++n)
 {
     Console.Write("\r");
     Console.Write(n + 1);
     // Sleep ...
 }
 for (int n = 0; n < duration; ++n)
 {
     Console.Write("\r");
     Console.Write(duration - n);
     // Sleep ...
 }
  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.