本文介绍了如何使文本框只接受数字和 Windows 8 中的一个小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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.

我一直在使用 Keydown Event,这是最好的还是我应该使用 Key up?

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

}

推荐答案

感谢大家的帮助.我终于做到了,并且在 Text changed 事件中效果很好.那是在将 inputscope 设置为 Numbers 之后

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

这篇关于如何使文本框只接受数字和 Windows 8 中的一个小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:55