WPF中TextBox限制输入方法
2010-03-22 11:23
267 查看
用PreviewKeyDown事件和KeyEventArgs.Handled 来实现
示例:限制了只能输入1和退格键
XAML
<TextBox HorizontalAlignment="Left" Margin="0,60,0,0" Name="textBox1" Width="120" Height="22" VerticalAlignment="Top" PreviewKeyDown="textBox1_PreviewKeyDown" />
C#
private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.D1 && e.Key != Key.Back && e.Key !=Key.NumPad1)
{
e.Handled = true;
}
}
示例:限制了只能输入1和退格键
XAML
<TextBox HorizontalAlignment="Left" Margin="0,60,0,0" Name="textBox1" Width="120" Height="22" VerticalAlignment="Top" PreviewKeyDown="textBox1_PreviewKeyDown" />
C#
private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.D1 && e.Key != Key.Back && e.Key !=Key.NumPad1)
{
e.Handled = true;
}
}
相关文章推荐
- [WPF]Show新窗口时TextBox等控件无法输入问题解决方法
- C# textBox限制输入的方法
- [WPF]Show新窗口时TextBox等控件无法输入问题解决方法
- [WPF]winfom中ShowWPF新窗口时TextBox等控件无法输入问题解决方法 .
- c# 限制textbox的输入范围和长度(长度不用maxlength方法)
- WPF限制TextBox只能输入数字
- 【WPF】自定义金额输入控件CurrencyEditor和限制输入控件FieldTextBox
- c# 限制textbox的输入范围和长度(长度不用maxlength方法)
- WPF C# VS2013 TextBox控件 限制只能输入数字
- 限制CEdit 只输入小数的两种方法
- 关于textbox或combobox限制输入内容
- 限制只能输入数值型的高级方法
- 控制 TextBox 输入时若是英文自动转大写或转小写的方法
- Android EditText限制输入字符类型的方法总结
- WPF中Textbox长度限制
- C# TextBox中只允许输入数字的解决方法
- JavaScript 限制文本框不可输入英文单双引号的方法
- JavaScript 限制文本框不可输入英文单双引号的方法
- 让TextBox只输入金额或数字的方法
- 限制input输入类型(多种方法实现)