Windows 8 Windows 8-只接受数字的文本框出错
这是我用C#编写的文本框,带有按键事件处理程序Windows 8 Windows 8-只接受数字的文本框出错,windows-8,textbox,numbers,Windows 8,Textbox,Numbers,这是我用C#编写的文本框,带有按键事件处理程序 private void TextBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e) { //ONLY ACCEPTS NUMBERS char c = Convert.ToChar(e.Key); if (!c.Equals('0') && !c.Equals('1') && !
private void TextBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
//ONLY ACCEPTS NUMBERS
char c = Convert.ToChar(e.Key);
if (!c.Equals('0') && !c.Equals('1') && !c.Equals('2') && !c.Equals('3') && !c.Equals('4') &&
!c.Equals('5') && !c.Equals('6') && !c.Equals('7') && !c.Equals('8') && !c.Equals('9'))
{
e.Handled = true;
}
}
它确实可以防止字母从a到z。但是,如果我输入像!@$%这样的符号^&*()还是接受。我缺少什么?您可以使用
但在复制/粘贴的情况下,这对您没有多大帮助
同时检查右侧的相关问题。比如说
更新 如果是信件,请尝试
e.Handled = Char.IsLetter(c);
你可以用
但在复制/粘贴的情况下,这对您没有多大帮助
同时检查右侧的相关问题。比如说
更新 如果是信件,请尝试
e.Handled = Char.IsLetter(c);
没有可靠的方法来处理向下键或向上键事件,因为出于某些奇怪的原因,移位4(即$符号)返回的不是键值4 最好的解决方法是在使用之前捕获值并检查它是否为数字,然后通知用户
var IsNumeric = new System.Text.RegularExpressions.Regex("^[0-9]*$");
if (!IsNumeric.IsMatch(edtPort.Text))
{
showMessage("Port number must be number", "Input Error");
return;
}
如果你想让你的用户知道美元符号现在是一个数字,那就再好不过了 没有可靠的方法来处理向下键或向上键事件,因为出于某种奇怪的原因,移位4(即$符号)返回的不是键值 最好的解决方法是在使用之前捕获值并检查它是否为数字,然后通知用户
var IsNumeric = new System.Text.RegularExpressions.Regex("^[0-9]*$");
if (!IsNumeric.IsMatch(edtPort.Text))
{
showMessage("Port number must be number", "Input Error");
return;
}
如果你想让你的用户知道美元符号现在是一个数字,那就再好不过了 试试这段代码。但有时您会看到您按下的字符,它会立即被删除。不是最好的解决方案,但足够了
private void TextBox_OnTextChanged(object sender, TextChangedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox == null)
return;
if (textBox.Text.Length == 0) return;
var text = textBox.Text;
int result;
var isValid = int.TryParse(text, out result);
if (isValid) return;
var selectionStart = textBox.SelectionStart;
var resultString = new string(text.Where(char.IsDigit).ToArray());
var lengthDiff = textBox.Text.Length - resultString.Length;
textBox.Text = resultString;
textBox.SelectionStart = selectionStart - lengthDiff;
}
试试这个代码。但有时您会看到您按下的字符,它会立即被删除。不是最好的解决方案,但足够了
private void TextBox_OnTextChanged(object sender, TextChangedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox == null)
return;
if (textBox.Text.Length == 0) return;
var text = textBox.Text;
int result;
var isValid = int.TryParse(text, out result);
if (isValid) return;
var selectionStart = textBox.SelectionStart;
var resultString = new string(text.Where(char.IsDigit).ToArray());
var lengthDiff = textBox.Text.Length - resultString.Length;
textBox.Text = resultString;
textBox.SelectionStart = selectionStart - lengthDiff;
}
这可能有用,这是我用过的
private void txtBoxBA_KeyDown(object sender, KeyRoutedEventArgs e)
{
// only allow 0-9 and "."
e.Handled = !((e.Key.GetHashCode() >= 48 && e.Key.GetHashCode() <= 57));
// check if "." is already there in box.
if (e.Key.GetHashCode() == 190)
e.Handled = (sender as TextBox).Text.Contains(".");
}
private void txtBoxBA_KeyDown(对象发送器,KeyRoutedEventArgs e)
{
//仅允许0-9和“”
e、 Handled=!((e.Key.GetHashCode()>=48&&e.Key.GetHashCode()这可能会有帮助,这是我使用的
private void txtBoxBA_KeyDown(object sender, KeyRoutedEventArgs e)
{
// only allow 0-9 and "."
e.Handled = !((e.Key.GetHashCode() >= 48 && e.Key.GetHashCode() <= 57));
// check if "." is already there in box.
if (e.Key.GetHashCode() == 190)
e.Handled = (sender as TextBox).Text.Contains(".");
}
private void txtBoxBA_KeyDown(对象发送器,KeyRoutedEventArgs e)
{
//仅允许0-9和“”
e、 Handled=!((e.Key.GetHashCode()>=48&&e.Key.GetHashCode()也忽略数字键盘:(也忽略数字键盘:(