自定义文本框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
public class ExTextBoxFloat : TextBox { protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); if (e.KeyChar != 8) { if (this.Text.Length == 4) e.Handled = true; if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != '.') e.Handled = true; if (e.KeyChar == '.' && this.Text.IndexOf('.') != -1) { e.Handled = true; }
if (e.KeyChar == '.' && this.Text == "") { e.Handled = true; }
if (e.KeyChar != '.' && this.Text == "0") { e.Handled = true; }
if (this.Text.IndexOf('.') != -1) { if (this.Text.Length - this.Text.IndexOf('.') - 1 == 2) { e.Handled = true; } } } } protected override void WndProc(ref Message m) { if (m.Msg == 0x0302) { m.Result = IntPtr.Zero; return; } base.WndProc(ref m); } }
|
调用
定义好文本框后,直接在工具箱界面中拖出定义好的新文本框控件即可。
![CSharpExtensionTextbox](https://zsource-1257448055.cos.ap-shanghai.myqcloud.com/img/CSharpExtensionTextbox.png)