locked
if textBox value is number RRS feed

  • Question

  • Hi all,

    I have one problem ... how can I check if value entered in the textBox is number or not ... so I wanna something like this ...

    If ( !textBox.Text.isNumeric )

    {

         textBox.Text = "0";

    }

    ... and now I can work with this value like with number

    thanks for answers

    Thursday, June 5, 2014 7:58 AM

Answers

  • You should avoid exceptions if you can. Among other things they are very expensive operations and thus bad for your application performance. 

    For more information follow the link below:

    http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control

    • Marked as answer by Anne Jing Thursday, June 12, 2014 3:04 AM
    Thursday, June 5, 2014 9:19 AM
  • Hello,

    An alternate is to use a custom TextBox control that only accepts numeric values as shown here. Either method used in a event on a regular TextBox or a custom TextBox do not guard against a copy operation from the Windows Clipboard which is something else to consider and deal with.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Anne Jing Thursday, June 12, 2014 3:04 AM
    Thursday, June 5, 2014 12:05 PM

All replies

  • You can do it this way:

    double numericValue;
    if (double.TryParse(textBox.Text, out numericValue))
    {
        // do stuff
    }

    Thursday, June 5, 2014 8:08 AM
  • Thank you ! But I tried this ...

    try{Convert.ToDouble(txtX31.Text);}
    catch { txtX31.Text = "0"; }

    ... and it works 

    Thursday, June 5, 2014 8:11 AM
  • You should not use exceptions as a construct in that way. Do it this way instead:

    double numericValue;
    if (!double.TryParse(textBox.Text, out numericValue))
    {
        txtX31.Text = "0";
    }

    Thursday, June 5, 2014 8:23 AM
  • OK, I changed it thanks ... but .. why is it not good to do it in my way?.. try - catch ?
    Thursday, June 5, 2014 9:14 AM
  • You should avoid exceptions if you can. Among other things they are very expensive operations and thus bad for your application performance. 

    For more information follow the link below:

    http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control

    • Marked as answer by Anne Jing Thursday, June 12, 2014 3:04 AM
    Thursday, June 5, 2014 9:19 AM
  • double value = 0; // If not numeric, default will be zero.
    double.TryParse(textBox.Text, out value); // Numeric will be parsed
    
    // Now use value as per need...

    TejasJ.

    Thursday, June 5, 2014 9:23 AM
  • You should not use exceptions as a construct in that way. Do it this way instead:

    double numericValue;
    if (!double.TryParse(textBox.Text, out numericValue))
    {
        txtX31.Text = "0";
    }

    Can't agree with that. The computers are SO FAST, that the difference, even if it is 30,000 times slower than the actual code, can not be noticeable by the user.

    So yes, I would use either solutions. The cost of speed is irrelevant.

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Noam B Thursday, June 5, 2014 8:03 PM
    Thursday, June 5, 2014 10:36 AM
  • Can't agree with that. The computers are SO FAST, that the difference, even if it is 30,000 times slower than the actual code, can not be noticeable by the user.

    I agree with you. In this case it doesn't matter. But if you get used to using exceptions in the wrong way you will sooner or later end up with code that actually do slow down your program. I've seen them used in collection operations where there were 16 million exceptions thrown in one pass, that took a while to execute...
    Thursday, June 5, 2014 10:52 AM
  • Hello,

    An alternate is to use a custom TextBox control that only accepts numeric values as shown here. Either method used in a event on a regular TextBox or a custom TextBox do not guard against a copy operation from the Windows Clipboard which is something else to consider and deal with.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Anne Jing Thursday, June 12, 2014 3:04 AM
    Thursday, June 5, 2014 12:05 PM
  • Yes, It is not noticeable by user here, but when you working on application which do the opperation realtime basis like Games, Financial apps, Exception in the code would take a time approx 10ms, and could delay your work. So its better to avoid Exception if you can...

    Jiten Patoliya Windows C# developer

    Friday, June 6, 2014 9:02 AM