如何使一个文本框只接受数字和只有一个小数点在Windows 8 [英] How to make a textBox accept only Numbers and just one decimal point in Windows 8
本文介绍了如何使一个文本框只接受数字和只有一个小数点在Windows 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Windows 8手机。我写一个计算器应用程序,只能接受在文本框中的数字,只是一个小数点。我如何阻止用户在文本框中的计算器不能处理的输入两个或更多的小数点。
我一直用keydown事件,就是最好还是我应该用钥匙呢?
私人无效textbox_KeyDown(对象发件人,System.Windows.Input.KeyEventArgs E){
}
解决方案
感谢所有帮助。我终于做到这一点,它在文本改变事件的工作非常出色。这是设置inputscope到数字后,
字符串迪卡= Textbox1.Text;
INT countDot = 0;
的for(int i = 0; I< dika.Length;我++)
{
如果(迪卡[I] =='')
{
countDot ++;
如果(countDot→1)
{
迪卡= dika.Remove(I,1);
我 - ;
countDot--;
}
}
}
Textbox1.Text =迪卡;
Textbox1.Select(dika.Text.Length,0);
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) {
}
解决方案
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文