Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建只接受数字的WPF文本框_C#_Wpf_Textbox - Fatal编程技术网

C# 创建只接受数字的WPF文本框

C# 创建只接受数字的WPF文本框,c#,wpf,textbox,C#,Wpf,Textbox,我想创建一个文本框,它只接受特定范围内的数值。 实现这种文本框的最佳方式是什么 我考虑派生TextBox并重写TextProperty的验证和强制。然而,我不知道如何做到这一点,并且我理解导出WPF控件通常是不推荐的。 编辑: 我需要的是一个非常基本的文本框,它可以过滤掉所有不是数字的按键。 实现此功能的最简单方法是处理TextBox.PreviewtOutput事件: private void textBox_PreviewTextInput(object sender, TextCompo

我想创建一个文本框,它只接受特定范围内的数值。 实现这种文本框的最佳方式是什么

我考虑派生TextBox并重写TextProperty的验证和强制。然而,我不知道如何做到这一点,并且我理解导出WPF控件通常是不推荐的。
编辑:
我需要的是一个非常基本的文本框,它可以过滤掉所有不是数字的按键。 实现此功能的最简单方法是处理TextBox.PreviewtOutput事件:

private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    int result;
    if (!validateStringAsNumber(e.Text,out result,false))
    {
        e.Handled = true;
    }
}
(validateStringAsNumber是我主要使用Int.TryParse的函数)


一些建议的解决方案可能更好,但对于我需要的简单功能,该解决方案是最容易、最快的实现,同时也满足了我的需要。

到目前为止,我看到的大多数实现都是使用事件来实现正确的掩码行为。从TextBox继承并使用附加属性。两者都使用.Net来提供正确的掩码行为,但如果您只想要一个简单的“仅限数字”文本框,则不需要此类。

总体而言,最好的方法是对类似文本框的文本框使用覆盖方法
OnKeyPressed
。下面是使用静态字符方法的覆盖代码
IsDigit

if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar)) e.Handled = true;
这就是你真正需要做的。

private void txt\u TextChanged(对象发送者,textchangedventargs e)
private void txt_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textBox = sender as TextBox;
    int iValue = -1;

    if (Int32.TryParse(textBox.Text, out iValue) == false)
    {
         TextChange textChange = e.Changes.ElementAt<TextChange>(0);
         int iAddedLength = textChange.AddedLength;
         int iOffset = textChange.Offset;

         textBox.Text = textBox.Text.Remove(iOffset, iAddedLength);
    }
}
{ TextBox TextBox=发送者作为TextBox; int-iValue=-1; if(Int32.TryParse(textBox.Text,out-iValue)==false) { TextChange TextChange=e.Changes.ElementAt(0); int iAddedLength=textChange.AddedLength; int iOffset=textChange.Offset; textBox.Text=textBox.Text.Remove(iOffset,iAddedLength); } }
以我的拙见,满足此要求的最佳方法是只使用
OnTextChanged
事件,因为它可以处理击键的数字,也可以处理剪贴板的复制+粘贴。我希望下面显示的VB代码能够对此有所帮助

Private subnumericbox\u TextChanged(发送者作为对象,e作为textchangedventargs)处理我。TextChanged
将缓冲区设置为新的StringBuilder
Dim索引为整数=Me.SelectionStart
对于我的每个C.Text
如果字符是数字(C),那么
Buffer.Append(C)
ElseIf Me.SelectionStart>Buffer.Length然后
索引-=1
如果结束
下一个
Me.Text=Buffer.ToString
Me.SelectionStart=索引
端接头

这是我的首选方法:

private void yearTxt_PreviewKeyDown(object sender, KeyEventArgs e)
{
  switch (e.Key)
  {
    case Key.D0:
    case Key.D1:
    case Key.D2:
    case Key.D3:
    case Key.D4:
    case Key.D5:
    case Key.D6:
    case Key.D7:
    case Key.D8:
    case Key.D9:
    case Key.NumLock:
    case Key.NumPad0:
    case Key.NumPad1:
    case Key.NumPad2:
    case Key.NumPad3:
    case Key.NumPad4:
    case Key.NumPad5:
    case Key.NumPad6:
    case Key.NumPad7:
    case Key.NumPad8:
    case Key.NumPad9:
    case Key.Back:
      break;
    default:
      e.Handled = true;
      break;
  }
}

在这里检查我关于这个主题的问题:或者类似这样的问题:private void txtNumeric_previewtdemiput(object sender,TextCompositionEventArgs e){if(e.Text.Any(c=>!char.IsDigit(c)))e.Handled=true;}当我使用上面的代码时,我检索到错误消息:“错误1'System.Collections.Generic.ICollection'不包含'ElementAt'的定义,并且找不到接受'System.Collections.Generic.ICollection'类型的第一个参数的扩展方法'ElementAt'(是否缺少using指令或程序集引用?)NewProduct.xaml.cs 104 51 MediaStore”“缺少什么引用?@Legato可能与.net Framework不同唯一的问题是它不允许您使用小数点(。
TextBox
只有
KeyDown
PreviewKeyDown
事件作为
KeyEventArgs
发送参数。”。而且它不包含
KeyChar
属性,只包含
Key
属性,该属性从
Keys
enum返回一个值。此外,如果有人想从剪贴板粘贴一个数字,该怎么办?如果您经常使用数字文本框,并且不想大量重复代码,您可以这样做:1。通过将Bryuns代码重命名为“numericTextBox”或类似名称来创建一个方法。2.调用“PreviewKeyDown”事件中的方法,如下所示:private void textBox1_PreviewKeyDown(对象发送方,KeyEventArgs e){numericTextBox(发送方,e);}这不会处理粘贴的文本或用户修改数字键和键入字符,如$%^。用户也不能使用箭头键移动插入符号。