Search code examples
c#windowstextboxwebstore

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


Solution

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