using System; using System.Drawing; using System.Windows.Forms; namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon { public partial class NumOnlyTextBox : EnterTextBox { private bool Negative1; public NumOnlyTextBox() { InitializeComponent(); } //通过标识来判断是否允许输入负数 public bool Negative { get { return Negative1; } set { Negative1 = value; } } private void NumOnlyTextBox_KeyPress(object sender, KeyPressEventArgs e) { // 允许输入:数字、退格键(8)、全选(1)、复制(3)、粘贴(22) if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 1 && e.KeyChar != 3 && e.KeyChar != 22) { e.Handled = true; } } } }