Search code examples
c#syntax-errorunhandled

getting error "value '0' is not a valid value"


EDITED:

i am writing an autoclicker and its almost done but it has a weird problem...

in btnStart event i have written:

if (listViewPositions.Items.Count == 1)
{
    ClickIntervalStr1 = (listViewPositions.Items[(1) - 1].SubItems[(3) - 1].Text).ToString();
    ClickIntervalNum1 = Convert.ToInt32(ClickIntervalStr1);
    LeftOrRightClickStr1 = (listViewPositions.Items[(1) - 1].SubItems[(4) - 1].Text)
        .ToString();
    SingleOrDoubleClickStr1 = (listViewPositions.Items[(1) - 1].SubItems[(5) - 1].Text)
        .ToString();
}

and in timer (clickprocess) i have written:

timerClickProcessStopAfterXTimes.Interval = ClickIntervalNum1;

but when i press start button it gets this error: "Value '0' is not a valid value for interval. interval must be greater than 0."

i am sure everything is ok but why is this error shows up?!

here is the pictures:

enter image description here

enter image description here

enter image description here

please help...

FIXED:

i just had to write 4 lines of codes of listViewPositions.Items.Count == 1 to listViewPositions.Items.Count == 2 too and also others...


Solution

  • "Value '0' is not a valid value for interval. interval must be greater than 0."

    The obvious reason is because of ClickIntervalNum1 = Convert.ToInt32(ClickIntervalStr1);. The value of ClickIntervalStr1 is evident of 0...

    Microsoft has this to say about the Interval property:

    The time, in milliseconds, between Elapsed events. The value must be greater than zero, and less than or equal to MaxValue. The default is 100 milliseconds.

    To fix this, just make sure it's greater than 0... (simple example):

     timerClickProcessStopAfterXTimes.Interval = ClickIntervalNum1 > 0 ? ClickIntervalNum1 : 100;
    

    References:

    Timer.Interval Property System.Timers