Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have One Text Box in my Form.I will Give Input Value on Text Box decimal Values Eg:(1.23) but user will give input Eg(1.23.0098) this is not correct format how can i stop multiple Dots(.)without accepted on my text box.Please Help me !!!  
Posted
Comments
BillWoodruff 27-Oct-14 3:36am    
In addition to checking out using a MaskedTextBox, as Mehdi Gholam suggests, also consider using a NumericUpDown Control.

Also, keep in mind that different languages may have different syntaxes for decimal numbers: see:

http://stackoverflow.com/q/5927498/133321

1,234,567.89 // English style
1.234.567,89 // French style
1 234 567,89 // German style
1234567.89 // English mathematical style
1234567,89 // European mathematical style
12.00
12.0
12

Hello


Try This


C#
private void TextBox1_GotFocus(object sender, RoutedEventArgs e)
       {
           try
           {
               TextBox1.Select(0, TextBox1.Text.Length);
           }
           catch (System.Exception ex)
           {

           }

       }

       private void TextBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
       {
           try
           {
               ValidateTextBox(TextBox1, e);
           }
           catch (System.Exception ex)
           {

           }

       }


       private void ValidateTextBox(System.Windows.Controls.TextBox oTextBox, System.Windows.Input.TextCompositionEventArgs e)
       {
           try
           {
               string sAfterDot = string.Empty;
               string sInputText = oTextBox.Text;
               char c = Convert.ToChar(e.Text);

               if (c == '.')
               {
                   if (oTextBox.Text.Contains("."))
                   {
                       e.Handled = true;
                   }
                   else
                   {
                       e.Handled = false;
                   }
               }
               else
               {
                   if (c == '-')
                   {
                       if (oTextBox.Text.Equals(oTextBox.SelectedText))
                       {
                           e.Handled = false;
                       }
                       else
                       {
                           if (oTextBox.Text.Contains("-"))
                           {
                               e.Handled = true;
                           }
                           else
                           {
                               if (!string.IsNullOrEmpty(oTextBox.Text) && oTextBox.Text.LastIndexOf("-") != 0)
                               {
                                   e.Handled = true;
                               }
                               else
                               {
                                   e.Handled = false;
                               }

                           }
                       }
                   }
                   else
                   {
                       if (Char.IsNumber(c))
                       {
                           e.Handled = false;
                       }
                       else
                       {
                           if (c == '-')
                           {
                               e.Handled = false;
                           }
                           else
                           {
                               e.Handled = true;
                           }
                       }
                   }
               }

               if (sInputText.Contains("."))
               {
                   sAfterDot = sInputText.Substring(sInputText.IndexOf("."), (sInputText.Length - sInputText.IndexOf(".")));
                   if (sAfterDot.Length > 2)
                   {
                       if (oTextBox.SelectionStart > sInputText.IndexOf("."))
                       {
                           e.Handled = true;
                       }
                   }
               }

               base.OnPreviewTextInput(e);

           }
           catch (System.Exception ex)
           {

           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900