2010-12-03 3 views
1

La zone de texte accepte uniquement les nombres et un seul point décimal.Comment n'accepter qu'un seul point décimal dans une zone de texte?

Par exemple, la zone de texte contient "12345.56". Lorsque la période est appuyée une deuxième fois sur le clavier, elle ne doit pas apparaître dans la zone de texte car la zone de texte contient déjà un point.

+0

Est-ce web ou des fenêtres? –

+0

Windows Forms, Web Forms (ASP.NET) ou WPF/Silverlight? – decyclone

Répondre

7
[0-9]+(\.[0-9][0-9]?)? 

Optez pour des expressions régulières.

3

Hande événement KeyPress et en supposant que ses fenêtres

void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 

     if (e.KeyChar == '.') 
     { 
      if (richTextBox1.Text.Contains('.')) 
       e.Handled = true; 
     } 
    } 
2
**//Only numeric with Two decimal place comes in textbox and if user want to enter decimal again or space it will not allowed.** 

if ((e.Key >= Key.D0 && e.Key <= Key.D9 || 
       e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 || e.Key == Key.Decimal || e.Key == Key.OemPeriod)) 
      { 
       string strkey = e.Key.ToString().Substring(e.Key.ToString().Length - 1, e.Key.ToString().Length - (e.Key.ToString().Length - 1)); 

       if (e.Key >= Key.D0 && e.Key <= Key.D9 || 
        e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) 
       { 

        TextBox tb = sender as TextBox; 
        int cursorPosLeft = tb.SelectionStart; 
        int cursorPosRight = tb.SelectionStart + tb.SelectionLength; 
        string result1 = tb.Text.Substring(0, cursorPosLeft) + strkey + tb.Text.Substring(cursorPosRight); 
        string[] parts = result1.Split('.'); 
        if (parts.Length > 1) 
        { 
         if (parts[1].Length > 2 || parts.Length > 2) 
         { 
          e.Handled = true; 
         } 
        } 
       } 

       if (((TextBox)sender).Text.Contains(".") && e.Key == Key.Decimal) 
       { 
        e.Handled = true; 
       } 
      } 
      else 
      { 
       e.Handled = true; 
      } 

      if (e.Key >= Key.A && e.Key <= Key.Z || 
        e.Key == Key.Space) 
      { 
       e.Handled = true; 
      } 

      if (Keyboard.Modifiers == ModifierKeys.Shift && e.Key == Key.OemPeriod) 
      { 
       e.Handled = true; 
      } 
0
private bool isValueEnteredExceed100(string textBoxValue, string inputText) 
{ 
    var countPeriod = textBoxValue.Count(x => x.ToString().Equals(".")); 
    if (countPeriod <= 1) 
    { 
     bool isValidNumber = AreAllValidNumericChars(inputText); 

     if (isValidNumber == true || inputText == ".") 
     { 
      double enterdValue; 
      bool returnValue = false; 
      if (textBoxValue != string.Empty || textBoxValue != "") 
      { 
       enterdValue = Convert.ToDouble(textBoxValue); 
       if (enterdValue > 0 && enterdValue <= 100) 
       { 
        returnValue = true; 
       } 
      } 
      return returnValue; 
     } 
     else 
     { 
      return isValidNumber; 
     } 
    } 
    else 
    { 
     return false; 
    } 
} 

private void AcceptedFromTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
{ 
    string textBoxValue = AcceptedFromTextBox.Text + e.Text; 
    if (textBoxValue == ".") 
    { 
     textBoxValue = "0" + e.Text; 
     AcceptedFromTextBox.Text = textBoxValue; 
    } 
    e.Handled = !isValueEnteredExceed100(textBoxValue, e.Text); 
    AcceptedFromTextBox.SelectionStart = AcceptedFromTextBox.Text.Length; 
} 


private bool AreAllValidNumericChars(string str) 
{ 
    Regex regex = new Regex(@"[0-9]$"); 
    return regex.IsMatch(str); 
} 
0

Un autre exemple,

private void txtPrice_KeyPress(object sender, KeyPressEventArgs e) 
{ 

    if (txtPrice.Text.Length == 0) 
    { 
     if (e.KeyChar == '.') 
     { 
      e.Handled = true; 
     } 
    } 
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46) 
    { 
     e.Handled = true; 
    } 
    if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1) 
    { 
     e.Handled = true; 
    } 
} 
0

code sur pression de touche de zone de texte

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') 
{ 
     e.Handled = true; 
} 
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) 
{ 
     e.Handled = true; 
} 
Questions connexes