wpf怎么让Textbox只能输入数字?

在网上看了好多,发现要么太啰嗦,要么不够完美:其实只需要两步:

1.禁掉输入法:

<Window x:Class="WpfModelViewApplication1.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
    Title="Main Window" Height="400" Width="800">
        <Grid x:Name="grid1">
            <TextBox x:Name="tb" Width="100" HorizontalAlignment="Right" Margin="0,164,122,128"  input:InputMethod.IsInputMethodEnabled="False"/>
        </Grid>
</Window>
第二步 采用正则表达式:

<Window x:Class="WpfModelViewApplication1.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
    Title="Main Window" Height="400" Width="800">

<Grid x:Name="grid1">
            <TextBox x:Name="tb" Width="100" HorizontalAlignment="Right" Margin="0,164,122,128" PreviewTextInput="tb_PreviewTextInput" input:InputMethod.IsInputMethodEnabled="False"/>
        </Grid>

</Window>

cs后台代码:

        

//using System.Text.RegularExpressions;

        private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e)

        {

            Regex re = new Regex("[^0-9.-]+");

            e.Handled = re.IsMatch(e.Text);

        }
时间: 2024-08-03 00:13:36

wpf怎么让Textbox只能输入数字?的相关文章

[.NET] 在WPF中实现TextBox只能输入数字

如题,实现此功能的方法有很多,如果使用的是MVVM的开发模式,该如何实现此功能? 很简单,为TextBox新建一个附加属性,就可以完美的解决此问题. 新建一个附件属性 IsOnlyAcceptFigure ,并为其添加属性回调函数即可. 代码如下: namespace WPF.Validation { public class TextBoxOnlyAcceptFigureBehavior { public static readonly DependencyProperty IsOnlyAcc

wpf 限制textbox只能输入数字及特殊键

原文:wpf 限制textbox只能输入数字及特殊键 使用vs2015进行wpf设计时,发现限制输入数字时,Keydown事件无法检测到空格的输入,后经过查资料调试,发现PreviewKeyDown可以满足检测输入所有值合法性的要求,我的textbox输入框中只允许输入(大小键盘中)的数字0-9,以及删除回车,左右方向键,tab键,删除键,下面代码为限制 private void MinSpaceTextBox_PreviewKeyDown(object sender, KeyEventArgs

C#设置textBox只能输入数字(正数,负数,小数)简单实现

/* *设置textBox只能输入数字(正数,负数,小数) */ public static bool NumberDotTextbox_KeyPress(object sender, KeyPressEventArgs e) { //允许输入数字.小数点.删除键和负号 if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e

Asp.net TextBox只能输入数字

原文:Asp.net TextBox只能输入数字 <asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execCommand('undo')" runat="server" Width="80px" onafterpaste="if(isNaN(value))execCommand('undo')"></asp:textbox> 其

winform 中TextBox只能输入数字

textBox1.KeyPress+=TextNumber_KeyPress; private void TextNumber_KeyPress(object sender, KeyPressEventArgs e) { var tb = (TextBox) sender; if (e.KeyChar != Convert.ToChar(13)) { if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) { MessageShow("只能

Asp.net控制TextBox只能输入数字

在Asp.net开发中,为了确保数据的正确性,经常要对用户输入的内容进行验证,比如说用户只能输入数字,不能输入中文和英文,这如何实现呢?下面我们来看看.    实现过程,在TextBox控件中加两个属性事件:<asp:TextBox ID="txtName" runat="server" Width="175px" style="ime-mode:disabled" onkeypress="if (event.

UWP textbox 只能输入数字

private void Testbox_TextChanged(object sender, TextChangedEventArgs e) {    var textbox = (TextBox)sender;    if (!Regex.IsMatch(textbox.Text, "^\\d*\\.?\\d*$") && textbox.Text != "")   {     int pos = textbox.SelectionStart -

C# TextBox 只能输入数字

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { TextBox txt = sender as TextBox; //屏蔽非法按键,只能输入小数 if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '-' || e.KeyChar == '.' || e.KeyChar == 8) { if (txt.Text.Contai

textbox只能输入数字或中文的常用正则表达式和验证方法

验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9][0-9]*)$ 验证有两位小数的正实数:^[0-9]+(.[0-9]{2})?$ 验证有1-3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$ 验证非零的正整数:^\+?[1-9][0-9]*$ 验证非零的负整数:^\-[1-9][0-9]*$ 验证非负整数(正整数 + 0):^\d