The Perfect Day

分享技术,编写未来

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 //KeyPress事件:当控件获得焦点,并且用户按下且释放键盘上的键后发生
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//文本框只接受数字的输入和小数点
        {
            
//IsNumber:指定字符串中位于指定位置的字符是否属于数字类别
            
//IsPunctuation:指定字符串中位于指定位置的字符是否属于标点符号类别
            
//IsControl:指定字符串中位于指定位置的字符是否属于控制字符类别
            if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled 
= true; //获取或设置一个值,指示是否处理过System.Windows.Forms.Control.KeyPress事件
            }
            
else if (Char.IsPunctuation(e.KeyChar))
            {
                
if (e.KeyChar == '.')
                {
                    
if (((TextBox)sender).Text.LastIndexOf('.'!= -1)
                    {
                        e.Handled 
= true;
                    }
                }
                
else
                {
                    e.Handled 
= true;
                }
            }
        }
posted on 2006-11-24 16:02  StephenJu  阅读(673)  评论(0编辑  收藏  举报