using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace CommonControl
{
public enum ValType
{
Double,
PositiveDouble,
PositiveDoubleAndZero,
Int,
PositiveInt,
PositiveIntAndZero
}
public partial class TextBoxNum : TextBox
{
#region 属性
private ValType valueType = ValType.Double;
[Description("值的类型"), Category("自定义")]
public ValType ValueType
{
get { return valueType; }
set { valueType = value; }
}
private double? maxValue;
[Description("最大值"), Category("自定义")]
public double? MaxValue
{
get { return maxValue; }
set { maxValue = value; }
}
[Description("值,double类型,只读"), Category("自定义")]
public double Value
{
get
{
double d;
try
{
d = Convert.ToDouble(this.Text);
}
catch
{
d = 0;
}
return d;
}
}
#endregion
#region 构造方法
public TextBoxNum()
{
InitializeComponent();
RegEvent();
}
private void RegEvent()
{
this.LostFocus += TextBoxNum_LostFocus;
this.KeyPress += TextBoxNum_KeyPress;
this.TextChanged += TextBoxNum_TextChanged;
}
public TextBoxNum(IContainer container)
{
container.Add(this);
InitializeComponent();
RegEvent();
}
#endregion
private void TextBoxNum_LostFocus(object sender, EventArgs e)
{
errorProvider1.SetError(this, "");
if (this.Text == "")
{
return;
}
double d;
try
{
switch (this.ValueType)
{
case ValType.Double:
case ValType.PositiveDouble:
case ValType.PositiveDoubleAndZero:
d = Convert.ToDouble(this.Text);
break;
case ValType.Int:
case ValType.PositiveInt:
case ValType.PositiveIntAndZero:
d = Convert.ToInt32(this.Text);
break;
default:
d = 0;
break;
}
}
catch
{
switch (this.ValueType)
{
case ValType.Double:
errorProvider1.SetError(this, "必须是数字");
break;
case ValType.PositiveDouble:
errorProvider1.SetError(this, "必须是大于0的数字");
break;
case ValType.PositiveDoubleAndZero:
errorProvider1.SetError(this, "必须是大于等于0的数字");
break;
case ValType.Int:
errorProvider1.SetError(this, "必须是整数");
break;
case ValType.PositiveInt:
errorProvider1.SetError(this, "必须是大于0的整数");
break;
case ValType.PositiveIntAndZero:
errorProvider1.SetError(this, "必须是大于等于0的整数");
break;
default:
break;
}
this.SelectAll();
this.Focus();
return;
}
if (this.MaxValue != null)
{
if (d > this.MaxValue)
{
this.Text = this.MaxValue.ToString();
}
}
switch (this.ValueType)
{
case ValType.PositiveDouble:
if (d <= 0)
{
errorProvider1.SetError(this, "必须是大于0的数字");
this.SelectAll();
this.Focus();
return;
}
break;
case ValType.PositiveDoubleAndZero:
if (d < 0)
{
errorProvider1.SetError(this, "必须是大于等于0的数字");
this.SelectAll();
this.Focus();
return;
}
break;
case ValType.PositiveInt:
if (d <= 0)
{
errorProvider1.SetError(this, "必须是大于0的整数");
this.SelectAll();
this.Focus();
return;
}
break;
case ValType.PositiveIntAndZero:
if (d < 0)
{
errorProvider1.SetError(this, "必须是大于等于0的整数");
this.SelectAll();
this.Focus();
return;
}
break;
}
}
private void TextBoxNum_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '.') return;//允许输入 小数点 .
if (e.KeyChar == '-') return;//允许输入 负号 -
if (e.KeyChar == '\b') return;//允许输入 退格
//数字0~9所对应的keychar为48~57
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;//取消KeyPress事件
}
}
private void TextBoxNum_TextChanged(object sender, EventArgs e)
{
errorProvider1.SetError(this, "");
}
}
}
WinForm 自定义控件 TextBoxNum只允许输入数字
最新推荐文章于 2024-08-04 03:08:13 发布