首先引用
using System.Text.RegularExpressions;
给TextBox添加KeyPress事件,代码如下:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
if ((Convert.ToInt32(e.KeyChar) == 8))
            
{
                e.Handled 
= false;
            }

            
else
            
{
                Regex numRegex 
= new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
                Match Result 
= numRegex.Match(Convert.ToString(e.KeyChar));
                
if (Result.Success)
                
{
                    e.Handled 
= false;
                }

                
else
                
{
                    e.Handled 
= true;
                }

            }

        }
posted on 2007-05-26 14:59  刺猬博客  阅读(1023)  评论(0编辑  收藏  举报