WPF TextBox 只能输入数字键
<Grid> <TextBox Name="textBox1" PreviewTextInput="textBox1_PreviewTextInput" HorizontalAlignment="Stretch" VerticalAlignment="Center" /> </Grid> |
|
1
2
3
4
5
6
|
//using System.Text.RegularExpressions; private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e) { Regex re = new Regex("[^0-9.-]+"); e.Handled = re.IsMatch(e.Text); } |
KeyDown事件:2011-04-28优化Tab And RightCtrl
private void tbCount_KeyDown(object sender, KeyEventArgs e)
{
TextBox txt = sender as TextBox; //屏蔽非法按键
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key.ToString() == "Tab")
{
if (txt.Text.Contains(".") && e.Key == Key.Decimal)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
{
if (txt.Text.Contains(".") && e.Key == Key.OemPeriod)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
if (e.Key.ToString() != "RightCtrl")
{
MessageBox.Show(this.Resources["Txt_InnerPage_ConnPointManage_TabMyConnPoint_AddMyCloudSeeNum_Prompt"].ToString(), this.Resources["Txt_InnerPage_ConnPointManage_TabMyConnPoint_AddMyCloudSeeNum_PromptTitle"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}
TextChanged事件:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
//屏蔽中文输入和非法字符粘贴输入
TextBox textBox = sender as TextBox;
TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, 0); int offset = change[0].Offset;
if (change[0].AddedLength > 0)
{
double num = 0;
if (!Double.TryParse(textBox.Text, out num))
{
textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
textBox.Select(offset, 0);
}
}
}
在网上有不少关入这方面的资料,下面是我选用的一个方案
public NumberTextBox()
{
InitializeComponent();
this.KeyDown += NumberTextBox_KeyDown;
this.TextChanged += NumberTextBox_TextChanged;
}
void NumberTextBox_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
void NumberTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, 0);
int offset = change[0].Offset;
if (change[0].AddedLength > 0)
{
double num = 0;
if (!Double.TryParse(this.Text, out num))
{
this.Text = this.Text.Remove(offset, change[0].AddedLength);
this.Select(offset, 0);
}
}
}
但是这个方案当输入法为中文的时候效果不理想,于是想到了禁用输入法。
引入xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
然后再 TextBox控件的xaml中 input:InputMethod.IsInputMethodEnabled="False" 就可以了。
WPF TextBox 只能输入数字键的更多相关文章
- wpf textbox只能输入数字,屏蔽中文输入
1.设置textbox属性InputMethod.IsInputMethodEnabled="False" 2.增加KeyDown事件 private void TextBox_K ...
- C#设置textBox只能输入数字(正数,负数,小数)简单实现
/* *设置textBox只能输入数字(正数,负数,小数) */ public static bool NumberDotTextbox_KeyPress(object sender, KeyPres ...
- Asp.net TextBox只能输入数字
原文:Asp.net TextBox只能输入数字 <asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execC ...
- TextBox只能输入数字
Asp.net TextBox只能输入数字 <asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execComm ...
- C# textbox中输入时加限制条件 // C#Winform下限制TextBox只能输入数字 // 才疏学浅(TextBox 小数点不能在首位+只能输入数字)
textbox中输入时加限制条件 分类: C# winform2008-08-26 08:30 306人阅读 评论(0) 收藏 举报 textbox正则表达式object 1.用正则表达式! 2.使用 ...
- c# TextBox只能输入数字的处理方法(完整版各种情况考虑在内,可根据需求灵活修改)
//选择文本框的事件窗口,找到按键输入的方法KeyPress,双击建立新的方法. /// <summary> /// textBox只能输入数字的处理方法 /// </summary ...
- [WinForm]TextBox只能输入数字或者正浮点型数字
关键代码: /// <summary> /// 只能输入数字[KeyPress事件] /// </summary> /// <param name="textB ...
- winform 中TextBox只能输入数字
textBox1.KeyPress+=TextNumber_KeyPress; private void TextNumber_KeyPress(object sender, KeyPressEven ...
- C# TextBox 只能输入数字
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { TextBox txt = sender as TextBox ...
随机推荐
- python系列七:Python3字典dict
#!/usr/bin/python #Python3 字典#字典是支持无限极嵌套的citys={ '北京':{ '朝阳':['国贸','CBD','天阶','我爱我家','链接地产 ...
- Python菜鸟之路:Django 数据库操作进阶F和Q操作
Model中的F F 的操作通常的应用场景在于:公司对于每个员工,都涨500的工资.这个时候F就可以作为查询条件 from django.db.models import F models.UserI ...
- JavaScript遍历IP段内所有IP
思路:将两个IP转换为数字进行比较,小的那个慢慢加一,直到变成大的那个IP所转换的数字,将这其中的数字再转换为IP地址即为IP段内所有的IP. //IP转数字 function ip2int(ip) ...
- 超出字数部分省略(主要解决不兼容;display: -webkit-box;的浏览器)
注明:内容于http://www.cnblogs.com/chentongtong/p/5474553.html进一步整理. 1.现webkit内核的浏览器支持display: -webkit-box ...
- 内置函数:min 用法
内置函数:min 用法 源码 def min(*args, key=None): # known special case of min """ min(iterable ...
- Android 屏幕适配扫盲、教程
转载请注明出处:http://blog.csdn.net/my_truelove/article/details/66584865 訪问 ruicb.com,一键抵达我的博客! 扫描左側或右下方二维码 ...
- Android测试读写sd卡文件与写sd卡文件耗时
测试从sd卡读1k大小的文件,再写1k大小的文件,由于处理耗时很短,所以循环500次,查看耗时:测试写1k大小的文件,直接在内存构造一个1k的buffer,将这个buffer直接写到文件,同样循环50 ...
- hadoop学习第三天-MapReduce介绍&&WordCount示例&&倒排索引示例
一.MapReduce介绍 (最好以下面的两个示例来理解原理) 1. MapReduce的基本思想 Map-reduce的思想就是“分而治之” Map Mapper负责“分”,即把复杂的任务分解为若干 ...
- sql server2005版本中,len函数计算了字符串末尾的空格
sql server2005版本中,len函数计算了字符串末尾的空格的长度,以下是测试脚本: print @@version declare @v varchar(max) set @v = 'hp, ...
- uuid的使用
1.mysql中直接使用uuid()函数,可以生成一个随机的uuid 正常的uuid是36位长度的,其中有4个字符是‘-’,在mysql中可以使用replace()函数来替换‘-’ insert in ...