C#中怎么简单的判断TextBox输入的是否为数字???

谢谢

这个不能简单判断。
比如:输入数字后可以删除,按方向键删除等。有很多情况。
我也试过了,自己写了一个控件,不是很理想,不过能用。
在winForm中可以直接使用。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace DataManageModule.Common
{
/// <summary>
/// 只能输入数字的文本框:TextBox
/// 说明:该文本框除了0~9、一个小数据点,上下左右键、删除键(Delete)、BackSpace键 能输入外其它的都不能输入
/// </summary>
public class NumberTextBox : TextBox
{
private ToolTip toolTip;//当输入非数字字符时,弹出的汽泡提示框
private System.ComponentModel.IContainer components;//系统本身
protected bool isAllowKeyPress;//是否允许按銉

private double maxValue = double.MaxValue;
[Description("输入数字的最大值")]
public double MaxValue
{
get { return maxValue; }
set { maxValue = value; }
}
private double minValue = double.MinValue;
[Description("输入数字的最小值")]
public double MinValue
{
get { return minValue; }
set { minValue = value; }
}

private bool isNegativeEnable = true;
/// <summary>
/// 是否允许为负数
/// </summary>
[Description("是否允许为负数:如果为Ture则可以为负数,否则只能是正数")]
public bool IsNegativeEnable
{
get { return isNegativeEnable; }
set { isNegativeEnable = value; }
}

/// <summary>
/// 构造函数,并初化值为0
/// </summary>
public NumberTextBox()
{
InitializeComponent();
this.Text = "0";
this.TextChanged += new EventHandler(NumberTextBox_TextChanged);
}

void NumberTextBox_TextChanged(object sender, EventArgs e)
{
double value;
if (this.Text == "" || this.Text=="-" || this.Text =="-0" || this.Text =="-0.")
{
return;
}
try
{
value = Convert.ToDouble(this.Text.Trim());
}
catch
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
if (value > maxValue || value < minValue)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据超出范围!", this, 1000);
this.Text = minValue.ToString();
return;
}
if (isNegativeEnable)
{

}
else
{
if (value < 0)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
}
}
/// <summary>
/// 重写OnKeyPress事件
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (isAllowKeyPress)//如果允许
{
e.Handled = true;
}
base.OnKeyPress(e);
}

protected override void OnLeave(EventArgs e)
{
if (this.Text.ToString().Trim() == "" || this.Text == "." || this.Text == "-"||this.Text=="-0" || this.Text=="-0.0" || this.Text=="-0")
{
this.Text = "0";

}
if (this.Text.ToString().IndexOf('.') == 0 && this.Text.Length > 1)
{
this.Text = "0" + this.Text;
}

if (this.Text.Trim().IndexOf("-.") != -1 && this.Text.Length > 3)
{
this.Text = this.Text.Insert(1, "0");
}
double value;
try
{
value = Convert.ToDouble(this.Text.Trim());
this.Text = value.ToString();
}
catch
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
if (value > maxValue || value < minValue)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据超出范围!", this, 1000);
this.Text = "0";
return;
}
if (isNegativeEnable)
{

}
else
{
if (value < 0)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
}
base.OnLeave(e);
}

protected override void OnKeyDown(KeyEventArgs e)
{

Rectangle rct = new Rectangle(new Point(0,0), new Size(this.Width, this.Height));
Point pt = PointToClient(Cursor.Position);
if (e.KeyValue.Equals(18) || (e.KeyValue >= 112 && e.KeyValue <= 123) || e.KeyValue == 145 || e.KeyValue == 45 || e.KeyValue == 36 || e.KeyValue == 33 || e.KeyValue == 34 || e.KeyValue == 16 || e.KeyValue == 20)
{
isAllowKeyPress = false;
return;
}
if (rct.Contains(pt))
{
Point p=PointToScreen(new Point(0,0));
Cursor.Position = new Point(p.X + this.Width + 1, p.Y + this.Height + 1);
}
if (e.KeyValue.Equals(46) || e.KeyValue.Equals(8) || e.KeyValue.Equals(39) || e.KeyValue.Equals(37) || e.KeyValue.Equals(38) || e.KeyValue.Equals(40))
{
isAllowKeyPress = false;
return;
}
else if ((e.KeyValue == 189) || (e.KeyValue == 109))
{
if (IsNegativeEnable)
{
if (this.Text == "")
{
isAllowKeyPress = false;
return;
}
else
{
int i = this.Text.ToString().IndexOf('-');
if (i != -1)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("已经存在负号了!", this, 1000);
isAllowKeyPress = true;
return;
}
else
{
isAllowKeyPress = false;
return;
}
}
}
else
{
isAllowKeyPress = true;
}

}
else if ((e.KeyValue == 46) || (e.KeyValue >= 96 && e.KeyValue <= 105) || (e.KeyValue.Equals(110)) || (e.Control == true) || (e.Control == true && (e.KeyValue.Equals(67) || e.KeyValue.Equals(86))) || (e.KeyValue == 8 || (e.KeyValue >= 48 && e.KeyValue <= 57)) || (e.KeyValue.Equals(8)) || (e.KeyValue.Equals(190)) || (e.KeyValue.Equals(13)))
{
string ss = this.Text;
int i = this.SelectionStart;
int j = this.Text.ToString().IndexOf('-');
int k = this.SelectedText.Length;
if (i == 0 && j!=-1 && k==0)
{
if (!(e.KeyValue.Equals(39) || e.KeyValue.Equals(40)))
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("不应该在负号前输入数字!.", this, 1000);
}
isAllowKeyPress = true;
}
else
{
isAllowKeyPress = false;
}
}
else
{
isAllowKeyPress = true;
this.toolTip.Show("", this, 0);
this.toolTip.Show("只能输入数字!", this, 1000);
}
if ((e.KeyValue.Equals(190) || e.KeyValue.Equals(110)))
{
int i=this.Text.ToString().IndexOf('.');
if (i == -1)
{
isAllowKeyPress = false;
return;
}
else
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("已经存在小数点了!", this, 1000);
isAllowKeyPress = true;
return;
}
//i = this.SelectionStart;
//int j = this.Text.ToString().IndexOf('-');
//if (i == 0 && j != -1)
//{
// if (!(e.KeyValue.Equals(39) || e.KeyValue.Equals(40)))
// {
// this.toolTip.Show("", this, 0);
// this.toolTip.Show("不应该在负号前输入数字!.", this, 1000);
// }
// isAllowKeyPress = true;
// return;
//}
//else
//{
// isAllowKeyPress = false;
// return;
//}
}
base.OnKeyDown(e);
this.Refresh();
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
this.toolTip.IsBalloon = true;
this.SuspendLayout();
//
// toolTip
//
this.toolTip.AutomaticDelay = 50;
this.toolTip.AutoPopDelay = 500;
this.toolTip.BackColor = System.Drawing.Color.White;
this.toolTip.ForeColor = System.Drawing.Color.Black;
this.toolTip.InitialDelay = 100;
this.toolTip.ReshowDelay = 0;
this.toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Error;
this.toolTip.ToolTipTitle = "错误";
this.ResumeLayout(false);

}
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-10-15
前台还是后台?

如果是前台可以直接使用验证控件+正则表达式

正则表达式:^\d+$(纯数字,不含小数点)
^\d+(.\d+)?$(含小数点)
正则表达式写法具体看你的要求,这个你可以参考正则表达式的具体说明

如果后台也可以使用正则表达式,不过我通常比较偷懒,我会直接使用异常处理。
try
{
double x=Convert.ToDouble(xx.Text.Trim());
//直接转换,如果是数字无异常,如果不是数字会抛异常

}
catch (Exception ex)
{

}本回答被提问者采纳
第2个回答  2007-10-15
你可以把用户输入的字符串每次取一个出来,然后判断是不是属于0-9,如果不是的话就证明不是数字了,我给的只是原理,代码你就自己写好了
第3个回答  2007-10-15
int num=-1;
if(int.TryParse(TextBox.Text,out num))
Response.Write("是数字");
else
Response.Write("不是数字");
第4个回答  2007-10-16
通常我也会异常处理。
try
{
double x=(double)(textBox1.Text);
//是数字
}
catch
{
//不是数字
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网