private
void
txtRoomNum_KeyDown(
object
sender, KeyEventArgs e)
{
TextBox txt = sender as TextBox;
// 屏蔽非法按键,只能输入整数
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
{
e.Handled = false ;
}
else
{
e.Handled = true ;
}
}
private void txtRoomArea_KeyDown( object sender, KeyEventArgs e)
{
TextBox txt = sender as TextBox;
// 屏蔽非法按键,只能输入小数
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
{
if (txt.Text.Contains( " . " ) && e.Key == Key.Decimal)
{
e.Handled = true ;
return ;
}
e.Handled = false ;
}
else
{
e.Handled = true ;
}
}
{
TextBox txt = sender as TextBox;
// 屏蔽非法按键,只能输入整数
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
{
e.Handled = false ;
}
else
{
e.Handled = true ;
}
}
private void txtRoomArea_KeyDown( object sender, KeyEventArgs e)
{
TextBox txt = sender as TextBox;
// 屏蔽非法按键,只能输入小数
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
{
if (txt.Text.Contains( " . " ) && e.Key == Key.Decimal)
{
e.Handled = true ;
return ;
}
e.Handled = false ;
}
else
{
e.Handled = true ;
}
}
参考一:
以下实现TextBox只能输入小数并且屏蔽中文输入和非法粘贴:
说明:以下实现均在Framework 3.0平台下
为TextBox加两个事件:TextChanged和KeyDown事件,具体如下:
KeyDown事件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
TextChanged事件
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。