using System; using System.Drawing; using System.Windows.Forms; namespace UAS_LabelMachine.CustomControl { public partial class NumOnlyTextBox : TextBox { 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; } } private void NumOnlyTextBox_Leave(object sender, EventArgs e) { this.BackColor = Color.White; } private void NumOnlyTextBox_Enter(object sender, EventArgs e) { this.BackColor = Color.GreenYellow; } } }