首页 > 代码库 > c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数
c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数
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 else55 {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 else66 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的限制条件,用户只能输入数字或小数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。