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 ...
随机推荐
- CRM客户关系管理系统-需求概设和详设
大概设计 大概设计就是对需求进行一个整体性分析,把需要实现的功能都列出来,对于客户关系管理系统,我们需要从角色出发,从而确定有哪些需求,最好是画个思维导图 首先我们是为培训学校这么一个场景来开发的,所 ...
- Python进阶(5)_进程与线程之协程、I/O模型
三.协程 3.1协程概念 协程:又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存 ...
- LeetCode:全排列【46】
LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...
- Centos----本地yum源制作
本地YUM源制作 1. YUM相关概念 1.1. 什么是YUM YUM(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的S ...
- 014_HDFS存储架构、架构可靠性分析、副本放置策略、各组件之间的关系
1.HDFS存储架构
- imx6solo wm8960始终没有声音输出
我尝试各种办法,wm8960始终不能得到声音输出.调试过程如下: 首先,打开电源使能脚: ret=gpio_request(SABRESD_CODEC_PWR_EN,"audio_pwr_e ...
- ubuntu 12.04.2 基于 L3.0.35_1.1.0_121218_source LTIB 问题汇总
1)解压L3.0.35_1.1.0_121218_source.tar.gz 2)cd L3.0.35_1.1.0_121218_source ,执行./install 3) 复制 patch-l ...
- 七 、linux正则表达式
为处理大量的字符串而定义的一套规则和方法 1)linux正则表达式以行为单位处理 2)alians grep = “grep –color=auto”,让匹配的内容显示颜色 3)注意字符集,expor ...
- 20145231《Java程序设计》第五次实验报告
实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验要求 基于Java Socket实现安全传输 基于TCP实现客户端和服 ...
- INSPIRED启示录 读书笔记 - 第15章 特约用户
产品开发伙伴 为了解决两个问题——既深入洞察目标用户的需求,又赢得用户对产品的推荐,建议征集特约用户协助完成产品研发 在项目的开始阶段物色至少六位积极.活跃.乐于分享的目标户,要求是他们在产品的目标用 ...