一.修正說明
主要是針對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:
//小數點
&nb //是否整數
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;
&nb 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
}
}