• <bdo id='kmgk0'></bdo><ul id='kmgk0'></ul>

      <small id='kmgk0'></small><noframes id='kmgk0'>

    1. <i id='kmgk0'><tr id='kmgk0'><dt id='kmgk0'><q id='kmgk0'><span id='kmgk0'><b id='kmgk0'><form id='kmgk0'><ins id='kmgk0'></ins><ul id='kmgk0'></ul><sub id='kmgk0'></sub></form><legend id='kmgk0'></legend><bdo id='kmgk0'><pre id='kmgk0'><center id='kmgk0'></center></pre></bdo></b><th id='kmgk0'></th></span></q></dt></tr></i><div id='kmgk0'><tfoot id='kmgk0'></tfoot><dl id='kmgk0'><fieldset id='kmgk0'></fieldset></dl></div>
    2. <legend id='kmgk0'><style id='kmgk0'><dir id='kmgk0'><q id='kmgk0'></q></dir></style></legend><tfoot id='kmgk0'></tfoot>

      1. 如何限制 C# 中的文本框只接收数字和(点“."或逗号“,"),在“."之后或“,&quo

        时间:2023-10-05

        <small id='r3LeN'></small><noframes id='r3LeN'>

        • <bdo id='r3LeN'></bdo><ul id='r3LeN'></ul>
          <tfoot id='r3LeN'></tfoot>

        • <legend id='r3LeN'><style id='r3LeN'><dir id='r3LeN'><q id='r3LeN'></q></dir></style></legend>

            1. <i id='r3LeN'><tr id='r3LeN'><dt id='r3LeN'><q id='r3LeN'><span id='r3LeN'><b id='r3LeN'><form id='r3LeN'><ins id='r3LeN'></ins><ul id='r3LeN'></ul><sub id='r3LeN'></sub></form><legend id='r3LeN'></legend><bdo id='r3LeN'><pre id='r3LeN'><center id='r3LeN'></center></pre></bdo></b><th id='r3LeN'></th></span></q></dt></tr></i><div id='r3LeN'><tfoot id='r3LeN'></tfoot><dl id='r3LeN'><fieldset id='r3LeN'></fieldset></dl></div>
                  <tbody id='r3LeN'></tbody>
                  本文介绍了如何限制 C# 中的文本框只接收数字和(点“."或逗号“,"),在“."之后或“,"只允许 2 个数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试开发一个代码来限制使用 C# 的 TextBox 只允许输入数字 + 逗号(,")或点(.") + 点或逗号后仅 2 个数字所以这样可以看到可能的数字可以进入:

                  i am trying develop a code to restrict TextBox using C# to only allow numbers entry + comma(",") or dot(".") + only 2 numbers after dot or comma So this way see possible numbers that can entry:

                  3213,04 = OK
                  3211,664 = Not
                  32.31 = OK
                  32.3214 = Not
                  334,,00 = Not
                  3247,.00 = Not
                  214.,00 = Not
                  32.. = Not
                  8465,0 = Ok
                  654.0 = Ok
                  

                  了解我的目标吗?我开发了下面的代码

                  Understood My goal ? I developed code bellow

                  private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
                  {
                      if (txtValormetrocubico.TextLength >= 0 && (e.KeyChar == (char)Keys.OemPeriod || e.KeyChar == (char)Keys.Oemcomma))
                      {
                          //tests 
                      }
                      else
                      {
                          if (!char.IsControl(e.KeyChar)
                              && !char.IsDigit(e.KeyChar)
                              && e.KeyChar != '.' && e.KeyChar != ',')
                          {
                              e.Handled = true;
                          }
                          // only allow one decimal point
                          if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                          {
                              e.Handled = true;
                          }
                  
                          if (e.KeyChar == ','  && (sender as TextBox).Text.IndexOf(',') > -1)
                          {
                              e.Handled = true;
                          }
                      }
                  }
                  

                  推荐答案

                  试试这个代码!我希望这有帮助.如果我能进一步帮助您,请告诉我.

                  Try this code ! I hope this helps. Let me know if I can assist you further.

                  这是我写的辅助函数

                  private bool alreadyExist(string _text , ref char KeyChar)
                          {
                              if (_text.IndexOf('.')>-1)
                              {
                                  KeyChar = '.';
                                  return true;
                              }
                              if (_text.IndexOf(',') > -1)
                              {
                                  KeyChar = ',';
                                  return true;
                              }
                              return false;
                          }
                  

                  这是您的按键事件处理程序

                  This your key press event handler

                   private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
                          {
                              if (!char.IsControl(e.KeyChar)
                                      && !char.IsDigit(e.KeyChar)
                                      && e.KeyChar != '.' && e.KeyChar != ',')
                              {
                                  e.Handled = true;
                              }
                  
                              //check if '.' , ',' pressed
                              char sepratorChar='s';
                              if (e.KeyChar == '.' || e.KeyChar == ',')
                              {
                                  // check if it's in the beginning of text not accept
                                  if (txtValormetrocubico.Text.Length == 0) e.Handled = true;
                                  // check if it's in the beginning of text not accept
                                  if (txtValormetrocubico.SelectionStart== 0 ) e.Handled = true;
                                  // check if there is already exist a '.' , ','
                                  if (alreadyExist(txtValormetrocubico.Text , ref sepratorChar)) e.Handled = true;
                                  //check if '.' or ',' is in middle of a number and after it is not a number greater than 99
                                  if (txtValormetrocubico.SelectionStart != txtValormetrocubico.Text.Length && e.Handled ==false)
                                  {
                                      // '.' or ',' is in the middle
                                      string AfterDotString = txtValormetrocubico.Text.Substring(txtValormetrocubico.SelectionStart);
                  
                                      if (AfterDotString.Length> 2)
                                      {
                                          e.Handled = true;
                                      }
                                  }
                              }
                              //check if a number pressed
                  
                              if (Char.IsDigit(e.KeyChar))
                              {
                                  //check if a coma or dot exist
                                  if (alreadyExist(txtValormetrocubico.Text ,ref sepratorChar))
                                  {
                                      int sepratorPosition = txtValormetrocubico.Text.IndexOf(sepratorChar);
                                      string afterSepratorString = txtValormetrocubico.Text.Substring(sepratorPosition + 1 );
                                      if (txtValormetrocubico.SelectionStart > sepratorPosition && afterSepratorString.Length >1)
                                      {
                                          e.Handled = true;
                                      }
                  
                                  }
                              }
                  
                  
                          }
                  

                  这篇关于如何限制 C# 中的文本框只接收数字和(点“."或逗号“,"),在“."之后或“,"只允许 2 个数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:FunctionsStartup vs IWebJobsStartup,在请求中读取 HttpHeaders 时出现问题 下一篇:将值从一个表单发送到另一个表单

                  相关文章

                • <i id='vGNCu'><tr id='vGNCu'><dt id='vGNCu'><q id='vGNCu'><span id='vGNCu'><b id='vGNCu'><form id='vGNCu'><ins id='vGNCu'></ins><ul id='vGNCu'></ul><sub id='vGNCu'></sub></form><legend id='vGNCu'></legend><bdo id='vGNCu'><pre id='vGNCu'><center id='vGNCu'></center></pre></bdo></b><th id='vGNCu'></th></span></q></dt></tr></i><div id='vGNCu'><tfoot id='vGNCu'></tfoot><dl id='vGNCu'><fieldset id='vGNCu'></fieldset></dl></div>
                  <legend id='vGNCu'><style id='vGNCu'><dir id='vGNCu'><q id='vGNCu'></q></dir></style></legend>

                    1. <small id='vGNCu'></small><noframes id='vGNCu'>

                    2. <tfoot id='vGNCu'></tfoot>

                      • <bdo id='vGNCu'></bdo><ul id='vGNCu'></ul>