private void InitializeComponent() { components = new System.ComponentModel.Container(); } #endregion public const int WM_CONTEXTMENU = 0x007b; //右鍵 public const int WM_CHAR = 0x0102; //text public const int WM_CUT = 0x0300; //delete public const int WM_COPY = 0x0301; //copy public const int WM_PASTE = 0x0302; public const int WM_CLEAR = 0x0303; public const int WM_UNDO = 0x0304;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
namespace ExControls
{
public class NumberText : System.Windows.Forms.TextBox
{
private System.ComponentModel.Container components = null; public NumberText(System.ComponentModel.IContainer container)
{
container.Add(this);
InitializeComponent();
} public NumberText()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region コンポーネント デザイナで生成されたコード
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion public const int WM_CONTEXTMENU = 0x007b; //右鍵
public const int WM_CHAR = 0x0102; //text
public const int WM_CUT = 0x0300; //delete
public const int WM_COPY = 0x0301; //copy
public const int WM_PASTE = 0x0302;
public const int WM_CLEAR = 0x0303;
public const int WM_UNDO = 0x0304;
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_CHAR:
System.Console.WriteLine(m.WParam);
bool isSign = ((int)m.WParam == 45);
bool isNum = ((int)m.WParam >= 48) && ((int)m.WParam <= 57);
bool isBack = (int)m.WParam == (int)Keys.Back;
//bool isDelete = (int)m.WParam == (int)Keys.Delete; //"."
bool isCtr = ((int)m.WParam == 24) || ((int)m.WParam == 22) || ((int)m.WParam == 26) ||((int)m.WParam == 3);
if( isNum || isBack || isCtr)
{
base.WndProc (ref m);
}
if (isSign)
{
if (this.SelectionStart!=0)
{
break;
}
base.WndProc (ref m);
break;
} if ((int)m.WParam == 1)
{
this.SelectAll();
}
break;
case WM_PASTE:
IDataObject iData = Clipboard.GetDataObject(); if(iData.GetDataPresent(DataFormats.Text))
{
string str = (string)iData.GetData(DataFormats.Text);
if (MatchNumber(str))
{
base.WndProc (ref m);
break;
}
}
m.Result = (IntPtr)0;
break;
default:
base.WndProc (ref m);
break;
}
}//end WndProc private bool MatchNumber(string ClipboardText)
{
int index=0;
string strNum = "-0.123456789";
index = ClipboardText.IndexOf(strNum[0]);
if (index>=0)
{
if (index>0)
{
return false;
}
index = this.SelectionStart;
if (index>0)
{
return false;
}
}
index = ClipboardText.IndexOf(strNum[2]);
if (index!=-1)
{
index = this.Text.IndexOf(strNum[2]);
if (index!=-1)
{
return false;
}
}
for(int i=0; i<ClipboardText.Length; i++)
{
index = strNum.IndexOf(ClipboardText[i]);
if (index <0)
{
return false;
}
}
return true;
}//end MatchNumber
}//end NumberText
}//end ExControls
{
if((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13)
{
e.Handled = true;
MessageBox.Show("只能为数字!");
}
}
这样就可以了
e.KeyChar < 48 || e.KeyChar > 57
这个条件就可以了