国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中設置textbox限制條件的方法

2019-10-29 21:47:12
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了C#中設置textbox限制條件的方法,可實現設置像數量、價格、金額等的textbox的限制條件,用戶只能輸入數字或小數,是非常實用的技巧,需要的朋友可以參考下
 

本文實例講述了C#中設置textbox限制條件的方法,分享給大家供大家參考。具體實現方法如下:

 

復制代碼代碼如下:
#region 設置數量等textbox控件樣式及限制條件(具體調用的方法就是重寫或直接調用ShieldNumberTextBoxOtherKeys函數)

 

/// <summary>
/// 屏蔽數字textbox的其他字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void tBoxNumbers_KeyDown(object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = true;

    switch (e.KeyCode)
    {
 case Keys.D0:
 case Keys.D1:
 case Keys.D2:
 case Keys.D3:
 case Keys.D4:
 case Keys.D5:
 case Keys.D6:
 case Keys.D7:
 case Keys.D8:
 case Keys.D9:
 case Keys.NumPad0:
 case Keys.NumPad1:
 case Keys.NumPad2:
 case Keys.NumPad3:
 case Keys.NumPad4:
 case Keys.NumPad5:
 case Keys.NumPad6:
 case Keys.NumPad7:
 case Keys.NumPad8:
 case Keys.NumPad9:
 case Keys.Back:
 case Keys.OemPeriod:
 case Keys.Delete:
 case Keys.Decimal:
     e.SuppressKeyPress = false;
     break;
 default:
     break;
    }
}

public virtual void tBoxNumbers_KeyPress(object sender, KeyPressEventArgs e)
{
    TextBox tBox = sender as TextBox;
    char c = e.KeyChar;

    if (c.ToString().Equals("."))
    {
 if (tBox.Text.Length <= 0)
     e.Handled = true;           //小數點不能在第一位     
 else
 {
     float f;
     float oldf;
     bool b1 = false, b2 = false;
     b1 = float.TryParse(tBox.Text, out oldf);
     b2 = float.TryParse(tBox.Text + e.KeyChar.ToString(), out f);
     if (b2 == false)
     {
  if (b1 == true)
      e.Handled = true;
  else
      e.Handled = false;
     }
 }
    }
}

/// <summary>
/// 屏蔽數字textbox的其他字符串
/// </summary>
/// <param name="tbox">要屏蔽的textbox</param>
public virtual void ShieldNumberTextBoxOtherKeys(TextBox tbox)
{
    tbox.ImeMode = ImeMode.Disable;
    tbox.KeyDown += tBoxNumbers_KeyDown;
    tbox.KeyPress += tBoxNumbers_KeyPress;
}

#endregion

 

希望本文所述對大家的C#程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武冈市| 平南县| 长顺县| 淅川县| 灌南县| 岗巴县| 常州市| 都匀市| 闻喜县| 昌邑市| 建瓯市| 武城县| 青浦区| 塔城市| 缙云县| 津南区| 茌平县| 兴义市| 高陵县| 江永县| 桦川县| 咸宁市| 元朗区| 磴口县| 博乐市| 南京市| 孟连| 齐齐哈尔市| 北票市| 临夏县| 安阳县| 长葛市| 虞城县| 玉田县| 长白| 商河县| 宝兴县| 曲松县| 深泽县| 峨边| 靖安县|