標籤: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 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的限制條件,使用者只能輸入數字或小數