Try Catch Statements

Try Catch Statement for Preventing non-integers

Page 1 of 1

8 Replies - 781 Views - Last Post: 02 June 2010 - 08:25 AM Rate Topic: -----

#1 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Try Catch Statements

Posted 01 June 2010 - 06:31 PM

Hi All,

I am wandeering if someone can help me to modify the following code to do the following:
1. Ensure that the values in either textBoxmin.Text, textBoxhrs.Text or textBoxsec.Text) can ONLY be integers (if another character is entered, an error is thrown)
2. Ensure that only if AT LEAST ONE INTEGER is entered in either of the 3 text blocks, the operation can proceed (if there are no values entered, an error is thrown)


                if ((textBoxhrs.Text != "") || (textBoxmin.Text != "") || (textBoxsec.Text != ""))
                    //If there is at least one value entered in any of the 3 boxes
                try 
                {
                        hours = System.Convert.ToInt32(textBoxhrs.Text);
                        minutes = System.Convert.ToInt32(textBoxmin.Text);
                        seconds = System.Convert.ToInt32(textBoxsec.Text);
                }
                catch 
                {
                    MessageBox.Show("Only numbers allowed!!!");


//HOWEVER - if I type in one integer, the error is still thrown, when instead the program should proceed)



Is This A Good Question/Topic? 0
  • +

Replies To: Try Catch Statements

#2 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: Try Catch Statements

Posted 01 June 2010 - 06:48 PM

Hi,

you can use int.TryParse to parse the string an catch the exception at the same time (if there is any).

int.TryParse(textBoxhrs.Text, out hours);



Then you can check if the integers contain values.
Was This Post Helpful? 0
  • +
  • -

#3 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Re: Try Catch Statements

Posted 01 June 2010 - 07:16 PM

Thanks! Can you please also help me out with how to catch the exception in case the value entry is not an integer with
int.TryParse(textBoxhrs.Text, out hours);


Was This Post Helpful? 0
  • +
  • -

#4 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: Try Catch Statements

Posted 01 June 2010 - 07:19 PM

Forgot to mention the TryParse method returns a bool indicating parsing success. You don't have to catch exceptions as this is done by the method.
Was This Post Helpful? 1
  • +
  • -

#5 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Re: Try Catch Statements

Posted 01 June 2010 - 07:44 PM

the following code did the trick!
int.TryParse(textBoxhrs.Text, out hours);
                int.TryParse(textBoxmin.Text, out minutes);
                int.TryParse(textBoxsec.Text, out seconds);

                if (hours >= 1 || (minutes >= 1) || (seconds >= 1))

                //If there is at least one value entered in any of the 3 boxes
                {
                    timer1.Enabled = true;
                    startbutton.Enabled = true; //START is ON
                    pausebutton.Enabled = true; //CHANGED-GOOD(should be allowed only IF start is paused)
                    stopbutton.Enabled = true; //STOP is not allowed (after timer starts...can pause!)
                    textBoxhrs.Enabled = false;
                    textBoxmin.Enabled = false;
                    textBoxsec.Enabled = false;
                }
                else
                {
                    MessageBox.Show("Enter at least one integer!");
                }
            }


Was This Post Helpful? 0
  • +
  • -

#6 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: Try Catch Statements

Posted 02 June 2010 - 04:11 AM

Here's some variation for you:

int hours;
            int minutes;
            int seconds;

            bool flagOne = int.TryParse(txtHours.Text, out hours);
            bool flagTwo = int.TryParse(txtMinutes.Text, out minutes);
            bool flagThree = int.TryParse(txtSeconds.Text, out seconds);

            if ( flagOne && flagTwo && flagThree )
            {
                MessageBox.Show("All values have been entered", "Message...");
            }
            else
            {
                MessageBox.Show("Enter all values.", "Please try again...");

                if ( !flagOne )
                {
                    txtHours.Focus();
                    txtHours.SelectAll();
                }
                else if ( !flagTwo )
                {
                    txtMinutes.Focus();
                    txtMinutes.SelectAll();
                }
                else if ( !flagThree )
                {
                    txtSeconds.Focus();
                    txtSeconds.SelectAll();
                }
            }


Was This Post Helpful? 1
  • +
  • -

#7 CasGrimes  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 97
  • Joined: 09-March 10

Re: Try Catch Statements

Posted 02 June 2010 - 08:14 AM

So what is tryparse's purpose? i always wondered this.
Was This Post Helpful? 0
  • +
  • -

#8 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Re: Try Catch Statements

Posted 02 June 2010 - 08:20 AM

View PostCasGrimes, on 02 June 2010 - 07:14 AM, said:

So what is tryparse's purpose? i always wondered this.



To test if the value can be parsed, ie is it a number or not.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Try Catch Statements

Posted 02 June 2010 - 08:25 AM

The purpose behind TryParse is that exception handling is somewhat computationally expensive, so to prevent having to catch the exception and handle it in the event of a bad input, one uses TryParse to do the work and save the exception being thrown.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1