Search code examples
c#windows-phone-8

How to make a textBox accept only Numbers and just one decimal point in Windows 8


I am new to windows 8 phone. I am writing a calculator app that can only accept numbers in the textbox and just a single decimal point. how do I prevent users from inputting two or more decimal Points in the text box as the calculator cant handle that.

I have been using Keydown Event, is that the best or should I use Key up?

private void textbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {

}

Solution

  • Thanks all for the help. I finally did this and it worked very well in the Text changed event. That was after setting the inputscope to Numbers

    string dika = Textbox1.Text;
        int countDot = 0;
        for (int i = 0; i < dika.Length; i++)
        {
            if (dika[i] == '.')
            {
                countDot++;
                if (countDot > 1)
                {
                    dika = dika.Remove(i, 1); 
                    i--;
                    countDot--;
                }
            }
        }
    Textbox1.Text = dika;
    Textbox1.Select(dika.Text.Length, 0);