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

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

Answers


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;
        }
    }

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;       
}

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


Need Your Help

.HTACCESS RewriteRule not working as expected

php apache .htaccess magento mod-rewrite

I have a magento website, that is currently operating. Within a subfolder of this site, I have placed a 3rd party application that also has its own HTACCESS file to handle routing for its applicati...

how to generate endless random objects in corona SDK?

random lua corona

so I am very new to coding in general and I am trying to make a vertically-scrolling endless runner which basically involves jumping onto platforms to stay alive.I want to generate the same platfor...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.