如何使文本框只允许整数值? [英] how to make textbox who only allow integer value?

查看:55
本文介绍了如何使文本框只允许整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的wpf应用程序中创建一个仅接受整数值的文本框.如果有人在[a-z]之间输入字符,则文本框将拒绝它.因此它将不会显示在文本框中

i want to make a textbox in my wpf application which will accept only integer values. if someone types characters between [a-z], the textbox will reject it. Thus it will not be displayed in the textbox

推荐答案

您可以处理PreviewTextInput事件:

You can handle the PreviewTextInput event:

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
  // Filter out non-digit text input
  foreach (char c in e.Text) 
    if (!Char.IsDigit(c)) 
    {
      e.Handled = true;
      break;
    }
}

这篇关于如何使文本框只允许整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆