勿喷,谢谢!!!
//允许输入整形数字以及退格键
using System.Windows.Forms;
namespace BaseDataMaintain.Views.Controls
{
/// <summary>
/// int input textbox
/// </summary>
public class TextIntNumBox : Telerik.WinControls.UI.RadTextBox
{
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
base.OnKeyPress(e);
//开头不允许出现.
if (SelectionStart == 0)
{
//if (e.KeyChar == '0')
//{
// e.Handled = true;
// return;
//}
if (e.KeyChar == '-')
{
e.Handled = false;
return;
}
if (e.KeyChar == '1'
|| e.KeyChar == '2'
|| e.KeyChar == '3'
|| e.KeyChar == '4'
|| e.KeyChar == '5'
|| e.KeyChar == '6'
|| e.KeyChar == '7'
|| e.KeyChar == '8'
|| e.KeyChar == '9'
|| e.KeyChar == '\b')
{
e.Handled = false;
return;
}
}
//0后只可以跟. add 2014.11.27
if (this.Text.Trim().StartsWith("0") && e.KeyChar != '\b')
{
e.Handled = true;
return;
}
//只能数字 + Backspace
if (e.KeyChar == '0'
|| e.KeyChar == '1'
|| e.KeyChar == '2'
|| e.KeyChar == '3'
|| e.KeyChar == '4'
|| e.KeyChar == '5'
|| e.KeyChar == '6'
|| e.KeyChar == '7'
|| e.KeyChar == '8'
|| e.KeyChar == '9'
//|| e.KeyChar == '.'
|| e.KeyChar == '\b')
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
//支持复制、剪贴、黏贴、Ctrl+A
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (keyData == (Keys.Control | Keys.A))
{
this.SelectAll();
}
if (keyData == (Keys.Control | Keys.C))
{
this.Copy();
}
if (keyData == (Keys.Control | Keys.X))
{
this.Cut();
}
if (keyData == (Keys.Control | Keys.V))
{
this.Paste();
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}