locked
just input Number

    Question

  • hi

    i want user just input number in textbbox (windows store c#)

    have textbox peroperty for it or i can it  by code?

    and what  code i need?


    Amir Ameri


    • Edited by AMIR ameri Sunday, November 30, 2014 8:53 PM
    Sunday, November 30, 2014 8:53 PM

Answers

  • Hi AMIR,

    Just use textchanges() event on textbox 

    private void textBox1_TextChanged(object sender, EventArgs e)
        {
    
            int num;
    
            try
            {
                num = int.Parse(textBox1.Text);  //here's your value
                //Sucess
            }
    
            catch (Exception exc)
            {
                //Not a number You can clear the textbox 
    
            }
        }

    Hope that helps 
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    ---------------------------------------

    M Prabath Maduranga Peiris
    Microsoft Partner  and MSP/MSA 
    Blogs : prabathsl.blogspot.com

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Monday, December 1, 2014 4:28 AM
  • The TextBox has a Text property that you can use to get or set the text in it:

    //get typed in text:
    string text = textBox1.Text;
    
    //set text:
    textBox1.Text = "test...";
    <TextBox x:Name="textBox1"/>
    

    You may want to convert the string to a number, for example an int or a double using the int.Parse or double.Parse method:

                int number;
                if(int.TryParse(textBox1.Text, out number))
                {
                    //do something with the number...
                }

    Please remember to close your threads by marking helpful posts as answer.

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Sunday, November 30, 2014 9:17 PM
  • and in xaml also set the InputScope to Number for the textbox

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Monday, December 1, 2014 6:39 AM
  • thank you very much.

    you show very good way to me.


    Amir Ameri

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Monday, December 1, 2014 3:34 PM

All replies

  • The TextBox has a Text property that you can use to get or set the text in it:

    //get typed in text:
    string text = textBox1.Text;
    
    //set text:
    textBox1.Text = "test...";
    <TextBox x:Name="textBox1"/>
    

    You may want to convert the string to a number, for example an int or a double using the int.Parse or double.Parse method:

                int number;
                if(int.TryParse(textBox1.Text, out number))
                {
                    //do something with the number...
                }

    Please remember to close your threads by marking helpful posts as answer.

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Sunday, November 30, 2014 9:17 PM
  • Hi AMIR,

    Just use textchanges() event on textbox 

    private void textBox1_TextChanged(object sender, EventArgs e)
        {
    
            int num;
    
            try
            {
                num = int.Parse(textBox1.Text);  //here's your value
                //Sucess
            }
    
            catch (Exception exc)
            {
                //Not a number You can clear the textbox 
    
            }
        }

    Hope that helps 
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    ---------------------------------------

    M Prabath Maduranga Peiris
    Microsoft Partner  and MSP/MSA 
    Blogs : prabathsl.blogspot.com

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Monday, December 1, 2014 4:28 AM
  • and in xaml also set the InputScope to Number for the textbox

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Monday, December 1, 2014 6:39 AM
  • thank you very much.

    you show very good way to me.


    Amir Ameri

    • Marked as answer by AMIR ameri Monday, December 1, 2014 8:56 PM
    Monday, December 1, 2014 3:34 PM