首页 > Windows开发 > 详细

C# TextBox 只能输入数字

时间:2015-09-22 16:32:28      阅读:239      评论:0      收藏:0      [点我收藏+]
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = sender as TextBox;
            //屏蔽非法按键,只能输入小数
            if ((e.KeyChar >= 0 && e.KeyChar <= 9) || e.KeyChar == - || e.KeyChar == . || e.KeyChar == 8)
            {
                if (txt.Text.Contains(".") && e.KeyChar == .)
                {
                    e.Handled = true;             
                }
                else if (e.KeyChar == -)
                {
                    if (txt.Text.Length != 0)
                    {
                        e.Handled = true;
                    }
                }             
            }          
            else
            {
                e.Handled = true;
            }
        }  

 

C# TextBox 只能输入数字

原文:http://www.cnblogs.com/profession/p/4828922.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!