标签:style blog io ar color os sp on div
1 #region 设置数量等textbox控件样式及限制条件(具体调用的方法就是重写或直接调用ShieldNumberTextBoxOtherKeys函数) 2 3 /// <summary> 4 /// 屏蔽数字textbox的其他字符串 5 /// </summary> 6 /// <param name="sender"></param> 7 /// <param name="e"></param> 8 public virtual void tBoxNumbers_KeyDown(object sender, KeyEventArgs e) 9 { 10 e.SuppressKeyPress = true; 11 12 switch (e.KeyCode) 13 { 14 case Keys.D0: 15 case Keys.D1: 16 case Keys.D2: 17 case Keys.D3: 18 case Keys.D4: 19 case Keys.D5: 20 case Keys.D6: 21 case Keys.D7: 22 case Keys.D8: 23 case Keys.D9: 24 case Keys.NumPad0: 25 case Keys.NumPad1: 26 case Keys.NumPad2: 27 case Keys.NumPad3: 28 case Keys.NumPad4: 29 case Keys.NumPad5: 30 case Keys.NumPad6: 31 case Keys.NumPad7: 32 case Keys.NumPad8: 33 case Keys.NumPad9: 34 case Keys.Back: 35 case Keys.OemPeriod: 36 case Keys.Delete: 37 case Keys.Decimal: 38 e.SuppressKeyPress = false; 39 break; 40 default: 41 break; 42 } 43 } 44 45 public virtual void tBoxNumbers_KeyPress(object sender, KeyPressEventArgs e) 46 { 47 TextBox tBox = sender as TextBox; 48 char c = e.KeyChar; 49 50 if (c.ToString().Equals(".")) 51 { 52 if (tBox.Text.Length <= 0) 53 e.Handled = true; //小数点不能在第一位 54 else 55 { 56 float f; 57 float oldf; 58 bool b1 = false, b2 = false; 59 b1 = float.TryParse(tBox.Text, out oldf); 60 b2 = float.TryParse(tBox.Text + e.KeyChar.ToString(), out f); 61 if (b2 == false) 62 { 63 if (b1 == true) 64 e.Handled = true; 65 else 66 e.Handled = false; 67 } 68 } 69 } 70 } 71 72 /// <summary> 73 /// 屏蔽数字textbox的其他字符串 74 /// </summary> 75 /// <param name="tbox">要屏蔽的textbox</param> 76 public virtual void ShieldNumberTextBoxOtherKeys(TextBox tbox) 77 { 78 tbox.ImeMode = ImeMode.Disable; 79 tbox.KeyDown += tBoxNumbers_KeyDown; 80 tbox.KeyPress += tBoxNumbers_KeyPress; 81 } 82 83 #endregion
c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数
标签:style blog io ar color os sp on div
原文地址:http://www.cnblogs.com/yuzhou133/p/4112539.html