一.修正说明
主要是针对0在控件中的输入情况做出了调整.在selectionstart = 0的时候,如果text.length ! =0,那么将限制对0的输入.同样,对于类似0.19,中光标位于0后边的情况,同样不允许输入0.
二.源代码
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Drawing;
using
System.Data;
using
System.Text;
using
System.Windows.Forms;

namespace
JcsExpLibary.Numeric_Textbox

...
{
public partial class JcsInputNum : TextBox

...{
private bool _isreadonly = true ; //是否允许回车代替TAB
private bool _isintegral = false ;//是否整数输入
private bool _EnPaste = true ;//是否允许粘贴
private bool _EnContextMenu = true ; //是否允许现实右键菜单
private int _PointNumber = -1 ;//为-1时允许输入任意位小数
private bool _isnegativenumber = false; //是否允许输入负数
public delegate void InputNumberTextBoxEvent();
public event InputNumberTextBoxEvent PasteEvent;
public JcsInputNum()

...{
InitializeComponent();
}

/**//// <summary>
/// 是否只读
/// </summary>
[Category("JCS属性"),Description("是否只读。")]
public bool Isreadonly

...{

get ...{ return this._isreadonly; }

set ...{ this._isreadonly = value; }
}

/**//// <summary>
/// 是否是整数
/// </summary>
[Category("JCS属性"), Description("是否整数。")]
public bool isintegral

...{

get ...{ return this._isintegral; }

set ...{ _isintegral = value; }
}

/**//// <summary>
/// 是否允许输入负数
/// </summary>
[Category("JCS属性"), Description("是否允许输入负数。")]
public bool IsNegativeNumber

...{

get ...{ return _isnegativenumber; }

set ...{ _isnegativenumber = value; }
}

/**//// <summary>
/// 精度位数控制(即:允许输入几位小数控制)
/// </summary>
[Category("JCS属性"), Description("精度位数控制,-1时允许输入任意位小数。")]
public int PointNumber

...{

get ...{ return _PointNumber; }

set ...{ this._PointNumber = value; }
}
[Category("JCS属性"), Description("是否允许粘贴。")]
public bool EnablePaste

...{

get ...{ return _EnPaste; }
set

...{
_EnPaste = value;
this.Invalidate();
}
}

/**//// <summary>
///
/// </summary>
[Category("JCS属性"), Description("是否右键菜单。")]
public bool EnableContextMenu

...{


get ...{ return _EnContextMenu; }
set

...{
_EnContextMenu = value;
this.Invalidate();
}
}



"private and overrides function"#region "private and overrides function"

/**//// <summary>
/// 检验输入
/// </summary>
/// <param name="e"></param>
/// <param name="KeyAsc"></param>
/// <param name="CurPos"></param>
private void ValidNumeric(System.Windows.Forms.KeyPressEventArgs e, int KeyAsc, int CurPos)

...{
switch (KeyAsc)

...{
// Backspace, Enter
case 8:
case 13:
break;
case 22:
if (!_EnPaste)
e.Handled = true;
break;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
//0-9 modified by jcs 2007-02-28 针对数字小数点精度的限制做了调整
// Allow entry
if (this._PointNumber != -1)

...{
int startindex = this.SelectionStart;
int index = this.Text.IndexOf(".");
if (index != -1)

...{
if (startindex > index)

...{
//如果光标位置在小数点右侧,则要判断是否已经输入了有效位的数字
int numlength = this.Text.Substring(index + 1).Length + 1;
if (numlength > this._PointNumber & this.SelectionLength == 0)

...{
e.Handled = true;
return;
}
}
//else if (startindex = index)
//{

//}
else if(KeyAsc ==48)

...{
//如果光标位置在小数点左侧,则允许输入
//判断是否0在最左侧或者前面的字符是第1个字符,而且也是0
if (startindex == 0 )

...{
e.Handled = true;
}
if (startindex == 1 && this.Text.Substring(0, 1) == "0")

...{
e.Handled = true;
}

}
}
else

...{
if (startindex == 0 && this.Text.Length !=0)

...{
e.Handled = true;
}
}
}
else if (KeyAsc ==48) //对精度无控制时对输入0的限制

...{
int startindex = this.SelectionStart;
int index = this.Text.IndexOf(".");
if (index != -1)

...{
//如果第1个字符是0,则不允许在0后面再次输入0
if (startindex == 1 && Text.Substring(0, 1) == "0")

...{
e.Handled = true;
}
else

...{
//判断在最左侧输入时,或者前面的字符是第1个字符,而且也是0
if (startindex == 0 && this.Text.Substring(0, 1) != ".")

...{
e.Handled = true;
}
}
}
else

...{
if (KeyAsc == 48)

...{
//如果字符长度不为空,并且在最左侧输入,0将被限制
if (startindex == 0 && this.Text.Length != 0)

...{
e.Handled = true;
}
else

...{
if (startindex == 0)

...{
this.Text = "0.0";
this.SelectionStart = 2;
this.SelectionLength = 1;
e.Handled = true;
}
}
}
}
}

break;
case 46:
//小数点
//是否整数
if (this._isintegral)

...{
e.Handled = true;
return; //
}

//小数点多于1个时
if (this.Text.IndexOf( ".") != -1)

...{
e.Handled = true;
}
else

...{
//在起始位置时
if (CurPos == 0)

...{
this.Text = "0" + this.Text;
//Add a zero before it
this.SelectionStart = 1;
//Set original cursor position光标在小数点后的位置
}

else if (CurPos == 1 & this.Text.StartsWith("-"))

...{
//负数
this.Text = "-0." + this.Text.Substring(1);
//Add zero before decimal连接小数点后的内容
this.SelectionStart = 3;
e.Handled = true;
}
else

...{
this.Text = this.Text + ".00";
//Append Decimal and two zero's 增添精确小数位数
this.SelectionStart = CurPos + 1;
this.SelectionLength = 2;
//Highlight the zero's
e.Handled = true;
}
}

break;
case 45:
//负号
if (!this._isnegativenumber)

...{
e.Handled = true;
return;
}
else

...{
if (CurPos != 0)

...{
//开始位置
e.Handled = true;
return;
}
}

break;
default:
e.Handled = true;

break;
}
}
//Validate Input data for Numeric
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)

...{

int KeyAsc;
//Ascii code of character
int CurPos;
//Cursor position

KeyAsc = Convert.ToInt32(e.KeyChar);
CurPos = this.SelectionStart;

ValidNumeric(e, KeyAsc, CurPos);

}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)

...{
if (_isreadonly)

...{
if (e.KeyCode == System.Windows.Forms.Keys.Enter)

...{
SendKeys.Send("{TAB}");
}
}
}

protected override void OnGotFocus(System.EventArgs e)

...{
this.Select();
this.SelectAll();
}
protected override void WndProc(ref System.Windows.Forms.Message m)

...{
switch (m.Msg)

...{
case 770:
//paste
if (!_EnPaste)
return;
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))

...{
string s = (string)iData.GetData(typeof(string));
if (!IsNumeric(s))

...{
return ;
}
}
else

...{
return ;
}
if (PasteEvent != null)

...{
PasteEvent();
}
break;
case 123:
//contextmenu
if (!_EnContextMenu)
return;
break;
case 256:
case 260:
//按下一个键,WM_KEYDOWN;WM_SYSKEYDOWN
//switch (m.WParam.ToInt32)
//{
// case Keys.Enter:
// SendKeys.Send("{tab}");
// break;
// case Keys.Left:

// return;
// case Keys.Right:

// return;
// case Keys.Up:

// return;
// case Keys.Down:

// return;
// case Keys.Shift + Keys.Tab:
// return;
// case Keys.Tab:
// break;

//}
break;

}
base.WndProc(ref m);
}

/**//// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public bool IsNumeric(string str)

...{
char[] ch = new char[str.Length];
ch = str.ToCharArray();
for (int i = 0; i < ch.Length; i++)

...{
if (ch[i] < 48 || ch[i] > 57)
return false;
}
return true;
}

#endregion


}

}