如何设置只有单点的文本框格式十进制值? [英] how to set text box format decimal values with single dot only?

查看:68
本文介绍了如何设置只有单点的文本框格式十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 !!!  

推荐答案

使用 MaskedTextBox http://msdn.microsoft.com/en-us/library/system .windows.forms.maskedtextbox.mask%28v = vs.110%29.aspx [ ^ ]


Hello





试试这个





Hello


Try This


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)
           {

           }
       }


这篇关于如何设置只有单点的文本框格式十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆