Reverse number in a string to count down

frendly Nub 1 Reputation point
2021-04-16T07:12:45.667+00:00

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?

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2021-04-17T06:04:00.657+00:00

    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
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.