how to make a text box only to accept numbers in c# for windows web store application using blank template

13.9k Views Asked by At

I am creating a windows store app in visual studio 12 , I am using c# language ,i have a text box ,but how to make it to accept only numbers ,if user tries to entry any other value than the number it should show an error message

3

There are 3 best solutions below

0
On BEST ANSWER

You can simply use try and catch like in following example:

private void textBox1_TextChanged(object sender, EventArgs e)
    {

        int num;

        try
        {
            num = int.Parse(textBox1.Text);  //here's your value
            label1.Text = num.ToString();
        }

        catch (Exception exc)
        {
            label2.Text = exc.Message;
        }
    }
0
On

You can use try and catch

or you can go for a little bit more code to determine whether input is number (int or double) or not by doing this

//---------------------------------------------------------------------------
bool TFmBatteryConfiguration::IsValidInt(char* x)
{
    bool Checked = true;

    int i = 0;
    do
    {
        //valid digit?
        if (isdigit(x[i]))
        {
            //to the next character
            i++;
            Checked = true;
        }
        else
        {
            //to the next character
            i++;
            Checked = false;
            break;
        }
    } while (x[i] != '\0');

    return Checked;
}

//---------------------------------------------------------------------------
bool TFmBatteryConfiguration::IsValidDouble(char* x)
{
    bool Checked = true;

    int i = 0;

    do
    {
        //valid digit?
        if (isdigit(x[i]))
        {
            //to the next character 
            i++;
            Checked = true;
        }
        else if (x[i] == '.')
        {
            //First character
            if (x[0] == '.')
            {
                Checked = false;
                break;    
            }
            else if (x[i] == '.' && x[i+1] == '\0')
            {
                Checked = false;
                break;
            }
            else
            {
                //to the next character
                i++;
            }
        }
        else
        {
            i++;
            Checked = false;
            break;
        }
    } while (x[i] != '\0');

    return Checked;
}

The code above is taken straight from one of my project in C++. but the idea is the same. C# is provided with char.isDigit()

1
On

In addition to the other answers, as you're writing a Windows Store App and will most likely be dealing with a virtual keyboard, you can make sure that you get a suitable keyboad view by setting the InputScope of the TextBox correctly (MSDN link here)

<TextBox InputScope="Number" .../>

There are a bunch of useful InputScope values described here.

Note that you will still need to do validation as described in the other answers, because you have to cater for the user overriding the displayed keyboard type or having an attached physical keyboard. I would do it with a KeyDown event handler, like so

private void TextBox_KeyDown_Number(object sender, KeyRoutedEventArgs e)
{
    if ((uint)e.Key >= (uint)Windows.System.VirtualKey.Number0 
        && (uint)e.Key <= (uint)Windows.System.VirtualKey.Number9)
    {
        e.Handled = false;
    }
    else e.Handled = true;       
}