打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Silverlight限制TextBox只能输入数字与小数的几种方法

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只能输入小数并且屏蔽中文输入和非法粘贴:
说明:以下实现均在Framework 3.0平台下
为TextBox加两个事件:TextChanged和KeyDown事件,具体如下:
KeyDown事件:
Code [http://www.xueit.com] 1private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
2        {
3            TextBox txt = sender as TextBox;
4
5             //屏蔽非法按键
6            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
7            {
8                if (txt.Text.Contains(".") && e.Key == Key.Decimal)
9                {
10                    e.Handled = true;
11                    return;
12                }
13                e.Handled = false;
14            }
15            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
16            {
17                if (txt.Text.Contains(".") && e.Key == Key.OemPeriod)
18                {
19                    e.Handled = true;
20                    return;
21                }
22                e.Handled = false;
23            }
24            else
25            {
26                e.Handled = true;
27            }
28        }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
只能输入数字的TextBox
c#中textbox只能输入数字
限制文本框只能输入数字
C# TextBox控件实现只能输入数字的方法
C# TextBox中只能输入数字的几种常用方法(C#)
WPF中TextBox控件对于鼠标单击获取焦点后的全选
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服