wpf怎么让Textbox只能输入数字?
2017-11-30 17:34
4091 查看
只需要两步:
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"
</Grid>
</Window>
直接改代码
cs后台代码:
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); }
相关文章推荐
- wpf怎么让Textbox只能输入数字?
- WPF TextBox只能输入数字
- wpf textbox控制只能输入数字
- WPF中,一个只能输入Double类型数字的TextBox控件的类
- WPF中自定义只能输入数字的TextBox
- WPF中自定义只能输入数字的TextBox
- WPF TextBox 只能输入数字键
- wpf textbox控制只能输入数字
- WPF中,一个只能输入Int类型数字的TextBox控件的类
- WPF中自定义只能输入数字的TextBox
- WPF C# VS2013 TextBox控件 限制只能输入数字
- WPF中自定义只能输入数字的TextBox
- WPF限制TextBox只能输入数字
- textbox里只能输入数字和按Backspace键
- 如何使TextBox只能输入数字?
- 一个只能输入数字的TEXTBOX
- 控制用户在textbox里只能输入数字
- 让textbox只能输入数字
- WinForm 限制TextBox只能输入数字和小数点
- Winform TextBox中只能输入数字的几种常用方法(C#)