none
C# timer Help

    Question


  • when I click the start button, it gives me Unhandled Exception Error.:Input string was not in correct format:Can you help?

    CODE

    //globally defined:
    string output_seconds;
            string output_minutes;
            int hour = DateTime.Now.Hour;
            int min = DateTime.Now.Minute;
            int sec = DateTime.Now.Second;



    private void Timer_tick(object sender, EventArgs e)
            {
               
               
                if (sec > 0)//keep moving
                {
                    sec--;
                }
                else if (min > 0)//convert minutes to seconds
                {
                    min--;
                    sec = +59;
                }
                else//time all gone stop timer
                {
                    timer1.Enabled = false;
                    time.Text = "done";
                   
               }


                if (sec > 59)//if user enters more than 60 sec
                {
                    min++;
                    sec = sec - 60;
                    return;
                }

               
                output_minutes = min.ToString();
                output_seconds = sec.ToString();
                output_minutes = output_minutes.PadLeft(2, Convert.ToChar("0"));
                output_seconds = output_seconds.PadLeft(2, Convert.ToChar("0"));

                time.Text = output_minutes + ":" + output_seconds;


            }

            private void button1_Click(object sender, EventArgs e)
            {
               
               sec = 0;
               min = 0;
               string temp = input.Text;
               temp = temp.PadLeft(4, Convert.ToChar("0"));
               string output_minutes = temp.Substring(0, 2);
               string output_seconds = temp.Substring(2);
               min = Int32.Parse(output_minutes);
               sec = Int32.Parse(output_seconds);
       if (sec > 59)
            {
                   min++;
                  sec = sec - 60;
             }
              
             

               timer1.Enabled = true;
            }

    Tuesday, November 20, 2007 4:48 PM

Answers

  • following code looks fishy. put a break point at last two lines, take a look at what input.Text is. your output_seconds probably is not in correct int format.

    also, you can use
    temp = temp.PadLeft(4, '0'); and use Int32.TryParse (.net 2.0) instead of Int32.Parse

    Code Block

              string temp = input.Text;
               temp = temp.PadLeft(4, Convert.ToChar("0"));
               string output_minutes = temp.Substring(0, 2);
               string output_seconds = temp.Substring(2);
               min = Int32.Parse(output_minutes);
               sec = Int32.Parse(output_seconds);

    Tuesday, November 20, 2007 6:02 PM

All replies

  • following code looks fishy. put a break point at last two lines, take a look at what input.Text is. your output_seconds probably is not in correct int format.

    also, you can use
    temp = temp.PadLeft(4, '0'); and use Int32.TryParse (.net 2.0) instead of Int32.Parse

    Code Block

              string temp = input.Text;
               temp = temp.PadLeft(4, Convert.ToChar("0"));
               string output_minutes = temp.Substring(0, 2);
               string output_seconds = temp.Substring(2);
               min = Int32.Parse(output_minutes);
               sec = Int32.Parse(output_seconds);

    Tuesday, November 20, 2007 6:02 PM
  • Also the default Interval for a Timer object is 100 or 1/10 or a second, so if you dont set timer1.Interval,

    the Tick event will be called roughly 10 times a second.  But perhaps thats what you want...

    Tuesday, November 20, 2007 11:47 PM