-1

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

arau
  • 23
  • 1
  • 2
  • 5
  • Please include some more details regarding your problem and what have you tried? – Mark May 02 '13 at 02:20
  • Welcome to [so]. Please do some research before asking questions. This is a duplicate of http://stackoverflow.com/questions/894263/how-to-identify-if-a-string-is-a-number – Jeremy Thompson May 02 '13 at 04:30
  • possible duplicate of [How to check if IsNumeric](http://stackoverflow.com/questions/9809340/how-to-check-if-isnumeric) – Rachel Gallen May 02 '13 at 07:40

3 Answers3

2

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;       
}
Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
0

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;
        }
    }
Evil Kot
  • 16
  • 1
0

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()

Thang Do
  • 316
  • 2
  • 16