Textbox input

I'm collecting numerical data from users via a textbox by using the following code.
double val=Convert::ToDouble(textBox1->Text);

How can I make sure an exception is thrown when someon tries to input other text characters or other non numerical characters


Answer this question

Textbox input

  • Tobey79

    Hi,

    I'd say it would be better to prevent user from entering an invalid character, rather than throwing an exception. You can use KeyPress event to filter out unwanted characters. The following code in C# will only allow entering numeric characters:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
       
    int number;
       
    string character = e.KeyChar.ToString();
       
    if (int.TryParse(character, out number) == false && character != "\b")
       
    {
           
    e.Handled =
    true;
       
    }
    }

    Andrej



  • Tryst

    I've just tried the following line in C# and it works fine:

    if (int.TryParse(character, out number) == false && character != "." && character != "\b")

    Can you set a breakpoint on the first line in the KeyPress event and after hitting the dot key, inspect what's the value of e.KeyChar (It should be the dot...)

    Andrej



  • Adhvika

    Actually, I changed from int to double and everything seem to work fine! Many thanks again for your help Andrej.

    double number;

    if (double::TryParse(character, number) == false && characterI2 != "\b")

  • A.Russell

    Andrej, the closest I could get to the decimal separator condition you suggested with c++ is as shown below but I was getting an error saying illegal reference to non-static member:

    if (int::TryParse(character, number) == false && character != "\b"&& character !=System::Globalization::NumberFormatInfo::CurrencyDecimalSeparator)

    I then used gcnew and the code compiled well. However, it is still blocking values such as '1.1' while allowing the '.' character when used on its own.

    System::Globalization::NumberFormatInfo^ nfi = gcnew System::Globalization::NumberFormatInfo;

    if (int::TryParse(character, number) == false && character != "\b"&& character !=nfi->CurrencyDecimalSeparator)

    There are very little chances that I would be using any character other than the dot, and I then thought that I could just write && character !="." as the last part of the condition but here also no luck.

    Any help would be greatly appreciated.


  • nlarkjason

    Thanks Andrej. I have modified it to suit the C++ syntax and the code looks something like this:

    int number;
    String ^character = Convert::ToString(textBox1->Text);
    if (int::TryParse(character, numberS) == false && character != "\b")
    {
    return;
    }

    However, I cannot input decimal values now as the dot is a character. Any help on tweaking the above condtition to allow for dots

  • Rama Bharti

    You could change the following line to include the current culture's decimal separator (may be a dot or a comma in different countries...):

    if (int::TryParse(character, number) == false && character != "\b" && character != System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator)

    Andrej



  • Textbox input