C# countdown timer: ArgumentOutOfRangeException

I want to code my app with the TimeSpan class for the timer countdown. I’d like to convert the TimeSpan countdown to the value in the progress bar just so the bar decrements as the timer ticks.

Right now it throws me ArguementOutOfRangeException (see the comment below inline my code). I’ve already ensured that all the UI controls (numericUpDown, ProgressBar) have the range of Minimum 0 and Maximum 100. I do have progressBarBeta.Value = 100; but this line does not trigger the exception. I debugged with and without this line, and got the same result for these two cases ( ArguementOutOfRangeException over and over again). Could you kindly point out what I went wrong here?

My current code is as follows:

TimeSpan ticksTotal;
TimeSpan ticksRemaining;


private void StartBtn_Click(object sender, EventArgs e)
    {
        bool startOk = true;

        //convert user inputs into int
        try
        {
            ticksRemaining = new TimeSpan(Convert.ToInt32(numHoursLeft.Value), Convert.ToInt32(numMinutesLeft.Value), Convert.ToInt32(numSecondsLeft.Value));
            progressBarBeta.Value = 100;


        }
        catch (FormatException exp)
        {
            startOk = false;
            MessageBox.Show(exp.Message);
        }

        //if timer is ready to start, it displays value 
        if(startOk == true)
        {
            Timer.Start();
            remainingTm.Text = ticksRemaining.ToString(@"hh\:mm\:ss");
        }
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        //subtract the elapsed time by 1 second
        ticksRemaining = ticksRemaining.Subtract(TimeSpan.FromSeconds(1));
        ticksTotal = ticksTotal - ticksRemaining;
        //display the remaining time after the subtraction
        remainingTm.Text = ticksRemaining.ToString(@"hh\:mm\:ss");
        ProgressBar();

        if (ticksRemaining.TotalSeconds <= 0)
        {
            Timer.Stop();
            TimerUp();

        }

    }

    private void ProgressBar()
    {
        
            if(ticksRemaining.TotalSeconds > 0)
            {
/* this throws: System.ArgumentOutOfRangeException Value of '-100' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.*/ 
progressBarBeta.Value = Convert.ToInt32(Math.Round((decimal)ticksRemaining.TotalSeconds / (decimal)ticksTotal.TotalSeconds) * 100);
                lblBar.Text = (100 - progressBarBeta.Value).ToString() + " %";
            }

    }

Caveat: I’m not great with C# and I can’t run your code, because you’ve provided a snippet of a larger C# GUI project…

I think your issue is here:

ticksTotal = ticksTotal - ticksRemaining;

ticksTotal is declared, but never initialised - at least not in the code you’ve provided - so the first time you reassign it to itself with a subtraction you’re making it negative. Continuing to subtract just makes a bigger negative number…

Again, not a C# user or able to reproduce your error (not enough code for that) but that’s my best guess going off what you’ve given me. :slight_smile:

Thank you so much - your tip was sufficient for me to resolve this error. I tweaked my ticksTotal variable (see the code snippet below). It now increments while my ticksRemaining variable decrements as the timer keep ticking.

And I also modified the value range of the progressBar under properties you can find in the design.cs . I actually raised the maximum value to 3600, so it won’t give me the out of range Exception whatever value I enters in my numericUpDown control. I use this control to incorporate user inputs. Fortunately the exception is gone.

        private void Timer_Tick(object sender, EventArgs e)
        {
            //subtract the elapsed time by 1 second
            ticksRemaining = ticksRemaining.Subtract(TimeSpan.FromSeconds(1));
            ticksTotal = ticksTotal.Add(TimeSpan.FromSeconds(1));
            //display the remaining time after the subtraction
            remainingTm.Text = ticksRemaining.ToString(@"hh\:mm\:ss");
            ProgressBar();

            if (ticksRemaining.TotalSeconds <= 0)
            {
                Timer.Stop();
                TimerUp();

            }

        }
1 Like

No problem, glad I was able to nudge you in the direction of a fix. :slight_smile:

1 Like