WPF中,一个只能输入Double类型数字的TextBox控件的类
来源:互联网 发布:mysql 创建联合索引 编辑:程序博客网 时间:2024/05/19 23:57
using System;
using System.Windows.Controls;
using System.Windows.Input;
namespace Common.Controls
{
public class TextBoxDoubleEx : TextBox
{
public TextBoxDoubleEx()
{
this.KeyDown += TextBoxEx_KeyDown;
this.TextChanged += TextBoxEx_TextChanged;
}
void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
{
//屏蔽中文输入和非法字符粘贴输入
var textBox = sender as TextBox;
var change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, 0);
int offset = change[0].Offset;
if (change[0].AddedLength > 0)
{
double num;
if (textBox != null && !Double.TryParse(textBox.Text, out num))
{
textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
textBox.Select(offset, 0);
}
}
}
void TextBoxEx_KeyDown(object sender, KeyEventArgs e)
{
var txt = sender as TextBox;
//屏蔽非法按键
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
{
if (txt != null && (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 != null && (txt.Text.Contains(".") && e.Key == Key.OemPeriod))
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
}
}
}
}
using System.Windows.Controls;
using System.Windows.Input;
namespace Common.Controls
{
public class TextBoxDoubleEx : TextBox
{
public TextBoxDoubleEx()
{
this.KeyDown += TextBoxEx_KeyDown;
this.TextChanged += TextBoxEx_TextChanged;
}
void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
{
//屏蔽中文输入和非法字符粘贴输入
var textBox = sender as TextBox;
var change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, 0);
int offset = change[0].Offset;
if (change[0].AddedLength > 0)
{
double num;
if (textBox != null && !Double.TryParse(textBox.Text, out num))
{
textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
textBox.Select(offset, 0);
}
}
}
void TextBoxEx_KeyDown(object sender, KeyEventArgs e)
{
var txt = sender as TextBox;
//屏蔽非法按键
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
{
if (txt != null && (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 != null && (txt.Text.Contains(".") && e.Key == Key.OemPeriod))
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
}
}
}
}
- WPF中,一个只能输入Double类型数字的TextBox控件的类
- WPF中,一个只能输入Int类型数字的TextBox控件的类
- WPF中自定义只能输入数字的TextBox
- WPF中自定义只能输入数字的TextBox .
- 让textbox控件中只能输入数字的代码
- 只能输入数字的TextBox自定义控件
- 只能输入数字的TextBox控件
- winform的textbox中只能输入数字和一个小数点
- 一个只能输入数字的TEXTBOX
- 一个只能输入数字的TEXTBOX
- WPF TextBox只能输入数字
- 只能输入数字的TextBox
- WPF C# VS2013 TextBox控件 限制只能输入数字
- 重写的一个TextBox控件,控制只能输入小数点和数字
- C#的winform中控制TextBox中只能输入数字?
- C#中TextBox只能输入数字的代码
- C#中TextBox只能输入数字的代码
- TextBox中只能输入数字的简单方法(C#)
- mlellipsis.js-实现多行文字溢出隐藏显示省略号
- java动态代理(JDK和cglib)
- uva 10369 - Arctic Network(最小生成树)
- python编程练习题
- 黑马程序员_泛型_5
- WPF中,一个只能输入Double类型数字的TextBox控件的类
- intelliJ +android sdk 开发环境的搭建
- 谢谢那些关注我的朋友
- 用keil写程序时出现“C(162): error C249: 'DATA': SEGMENT TOO LARGE“的错误
- net4.0安装失败
- CUDA: Wizard环境下CUDA DLL开发流程
- mysql update select 问题
- hive分区
- android中获取屏幕相关信息