c#中設定像數量,價格,金額等的textbox的限制條件,使用者只能輸入數字或小數

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.