C#中限定文本框中输入数据并要求

并要求能使用BackSpace键和空格键?

下面是我写的一个只能输入数字的文本框的控件。你可能用的到。
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-16
来一个简单的,只能输入0~9,小数点,空格,退格。

public class MyTextBox : TextBox
{
const int WM_CHAR = 0x0102;
const int VK_BACK = 0x08;
const int VK_SPACE = 0x20;

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CHAR && m.WParam.ToInt32() != '.' && (m.WParam.ToInt32() < '0' || m.WParam.ToInt32() > '9') && m.WParam.ToInt32() != VK_BACK && m.WParam.ToInt32() != VK_SPACE)
{
return;
}
base.WndProc(ref m);
}
}
相似回答