using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace AIMS.OremrUserControl { public partial class NumTextBox : TextBox { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.SuspendLayout(); // // NumTextBox // this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.NumTextBox_KeyPress); this.ResumeLayout(false); } #endregion public NumTextBox() { InitializeComponent(); } private void NumTextBox_KeyPress(object sender, KeyPressEventArgs e) { TextBox tb = sender as TextBox; int n = tb.Text.LastIndexOf("."); int j = tb.Text.LastIndexOf("-"); if (e.KeyChar < 48 || e.KeyChar > 57) { if (e.KeyChar != 46 && e.KeyChar != 8 && e.KeyChar != 13) { e.Handled = true; } if (e.KeyChar == 45 && j != -1) { e.Handled = true; } } if (e.KeyChar == 46 && n >= 0) { e.Handled = true; } //if (e.KeyChar == 13) //{ // SendKeys.Send("{tab}"); //} if (e.KeyChar == 8) { return; } if (n < 0 && tb.Text.Length > 5) { e.Handled = true; } if (tb.Text.Length > 7) { e.Handled = true; } if (n > 0 && n < tb.Text.Length - 2) { e.Handled = true; } } } }