2014-09-22 40 views
0

我正在開發一個使用c#的winform應用程序我已經成功實現了一種將文本框限制爲兩位小數的方法。我怎樣才能做到一位小數。 ? 。將文本框限制爲1位小數。 Winform C#

我爲小數點後兩位碼\

private void txtHraRep_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (char.IsNumber(e.KeyChar) || e.KeyChar == '.') 
      { 
       if (Regex.IsMatch(
       txtHraRep.Text, 
       "^\\d*\\.\\d{2}$")) e.Handled = true; 
      } 
      else e.Handled = e.KeyChar != (char)Keys.Back; 
     } 

更改爲 「^ \ d * \ \ d {1} $」))e.Handled = TRUE;

輸出

This is the output

+2

如何 「^ \\ d * \\ d {1} $。」 – bit 2014-09-22 06:40:20

+0

我嘗試了前面。它沒有工作。它允許小數點以後超過1個數字。 – user3859356 2014-09-22 06:43:29

+0

請建議一種方法來限制用戶在十進制後輸入不超過1的值 – user3859356 2014-09-22 06:49:08

回答

3

你可以只檢查其中小數點分隔符是在你的文本,然後確保比字符串的長度小於2(小數點後一位做這沒有正則表達式地方和1少陣列長度)

var decSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; 
var idx = txtBasic.Text.IndexOf(decSeparator); 
if(idx + 2 >= txtBasic.Text.Length) 
    ... 
0

我只是想

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     { 
      if (char.IsNumber(e.KeyChar) || e.KeyChar == '.') 
      { 
       if (Regex.IsMatch(
       textBox1.Text, 
       "^\\d*\\.\\d{1}$")) e.Handled = true; 
      } 
      else e.Handled = e.KeyChar != (char)Keys.Back; 
     } 
    } 

和它的工作,因爲它應該。它將輸入限制在小數點後的一位數字。 但是,您可以輸入多個小數點,然後輸入更多數字。 所以,你可以嘗試

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (char.IsNumber(e.KeyChar) || ((e.KeyChar == '.') && (textBox1.Text.IndexOf('.')== -1))) 
     { 
      if (Regex.IsMatch(
       textBox1.Text, 
       "^\\d*\\.\\d{1}$")) e.Handled = true; 
     } 
     else e.Handled = e.KeyChar != (char)Keys.Back; 
    } 

或類似

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (char.IsNumber(e.KeyChar) || ((e.KeyChar == '.') && (textBox1.Text.IndexOf('.')== -1))) 
     { 
      if (textBox1.Text.IndexOf('.') > 0) 
      { 
       if (textBox1.Text.IndexOf('.') < textBox1.Text.Length - 1) 
        e.Handled = true; 
      } 
     } 
     else e.Handled = e.KeyChar != (char)Keys.Back; 
    } 
+0

好吧,你會覺得你的甲方看來對我好 – user3859356 2014-09-22 07:20:41

+0

這個建議是否有效? – gottsche 2014-09-23 06:26:58

0

創建一個新的文本框繼承的TextBox像

[DefaultBindingProperty("Text")] 
[DefaultProperty("Text")] 
[DefaultEvent("ValueChanged")] 
public class SpecializedTextBox : TextBox 
{ 
    private bool _allowNegativeSign = false; 
    public bool AllowNegativeSign 
    { 
     get { return _allowNegativeSign; } 
     set { _allowNegativeSign = value; } 
    } 
    public decimal? DecimalValue 
    { 
     get 
     { 
      decimal k; 
      if (decimal.TryParse(this.Text, out k)) 
       return k; 
      else 
       return null; 
     } 
     set 
     { 
      if (value.HasValue) 
       this.Text = value.Value.ToString(); 
      else 
       this.Text = ""; 
     } 
    } 
    private void This_TextChanged(object sender, EventArgs e) 
    { 
     string s = base.Text; 
     int cursorpos = base.SelectionStart; 
     bool separatorfound = false; 
      for (int i = 0; i < s.Length;) 
      { 
       if (char.IsNumber(s[i])) 
        i++; 
       else if (AllowNegativeSign && i < System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.NegativeSign.Length && s.StartsWith(System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.NegativeSign)) 
        i++; 
       else if (!separatorfound && s[i].ToString() == System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator) 
       { 
        separatorfound = true; 
        i++; 
       } 
       else 
       { 
        s = s.Remove(i, 1); 
        if (i < cursorpos) 
         cursorpos--; 
       } 
      } 
     if (base.Text != s) 
     { 
      base.Text = s; 
      base.SelectionStart = cursorpos; 
      base.SelectionLength = 0; 
     } 
     if (ValueChanged != null) 
      ValueChanged(this, EventArgs.Empty); 
    } 
    public event EventHandler ValueChanged; 

    private void InitializeComponent() 
    { 
     this.SuspendLayout(); 
     // 
     // SpecializedTextBox 
     // 
     this.TextChanged += new System.EventHandler(this.This_TextChanged); 
     this.ResumeLayout(false); 
    } 
    public SpecializedTextBox() 
     : base() 
    { 
     InitializeComponent(); 
    } 
} 

現在使用這個文本框,並使用DecimalValue設置或獲取您的價值

0

嘗試:

List<string> doubleList = new List<string>(new string[] 
     { 
      "12345", 
      "1234.5", 
      "123.45", 
      "12.345", 
      "1.2345", 
      "1.2", 
      "1.23", 
      "1.234", 
      "1.23.45", 
      "12.3", 
      "123.4", 
     }) { }; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     foreach (var x in doubleList) 
     { 
      int countNumber = Regex.Matches(x, @"[0-9]").Count; 
      int countOfDot = Regex.Matches(x, @"\.").Count; 

      if (countOfDot == 1 && countNumber != 0) //contains "." and any digit 
      { 
       Console.WriteLine(x); 
      } 
      else if (countOfDot == 0 && countNumber != 0) //not contains "." and any digit 
      { 
       Console.WriteLine(x); 
      } 
      else 
      { 
       //do nothing . . . 
      } 
     } 
    } 

輸出:

all except for **1.23.45** (2dots)