【问题标题】:Limit Numbers after Decimal on Key Press Event按键事件小数后的限制数字
【发布时间】:2011-08-31 15:09:27
【问题描述】:

我使用以下代码只取用户的数字和一个小数点,这对我在 KeyPress 事件上工作正常:

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;
}

现在我想限制小数点/点后的数字/数字,即 35.25468,这意味着点/小数点后只需要 6 个数字/数字。

更新我!

【问题讨论】:

  • 虽然可以这样做,但我强烈反对它,因为如果用户尝试编辑该字段,这会让他们非常沮丧。它还会使您的代码相当复杂。如果您在用户退出该字段或提交表单后进行验证,您会更好。使用内置的验证东西。这就是它的用途。

标签: c# numbers decimal limit digit


【解决方案1】:
private void price_tb_KeyPress(object sender, KeyPressEventArgs e)
        {

        if (!char.IsControl(e.KeyChar) && !char.IsDigit(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 (!char.IsControl(e.KeyChar))
        {

        TextBox textBox = (TextBox)sender;

        if (textBox.Text.IndexOf('.') > -1 &&
                 textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
        {
            e.Handled = true;
        }

        }

    }

此代码将为您提供帮助。只占小数点后一位,小数点后两位,您可以进行相应的更改。

【讨论】:

    【解决方案2】:

    您可以像这样添加额外的检查

    TextBox textBox = (TextBox) sender;
    
    if (textBox.Text.IndexOf('.') > -1 &&
             textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >=3)
    {
        e.Handled = true;
    }
    

    注意,子字符串将包含“.”因此支票是>=3

    【讨论】:

      【解决方案3】:

      在按键事件和/或验证事件中,计算小数点后的字符数。在按键时,抑制它。在验证时,删除多余的小数位。确保您从 NumberFormatInfo 获取小数点字符,并非所有文化都使用“.”,即。在法国,他们的小数点实际上是一个逗号

      【讨论】:

      • 确保您从 NumberFormatInfo 获取小数点字符,并非所有文化都使用“.”,即。在法国,他们的小数点实际上是一个逗号
      【解决方案4】:

      在按键时,格式化字符串并将textBox.Text设置为格式化字符串。

      TextBox.Text = String.Format("{0:N3"}", textBox.Text)
      

      这种特殊格式会截断小数点后第三位的数字。

      【讨论】:

        【解决方案5】:

        我有textBox.SelectionLength == 0 允许修改选定的文本:

        private void price_tb_KeyPress(object sender, KeyPressEventArgs e) {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') {
                e.Handled = true;
            }
            TextBox textBox = (TextBox)sender;
            // only allow one decimal point
            if (e.KeyChar == '.' && textBox.Text.IndexOf('.') > -1) {
                e.Handled = true;
            }
            if (!char.IsControl(e.KeyChar) && textBox.SelectionLength == 0) {
                if (textBox.Text.IndexOf('.') > -1 && textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3) {
                    e.Handled = true;
                }
            }
        }
        

        【讨论】:

          【解决方案6】:

          我对Both FM的回答的问题是,当你输入一个小数位和两个小数时,你不能编辑文本。

          此代码也需要一个负数。

              private void TextBoxAmount_KeyPress(object sender, KeyPressEventArgs e)
              {
                  if (char.IsDigit(e.KeyChar))
                  {
                      // OK, but not more than 2 after the [.]
                      if (((TextBox)sender).Text.Contains('.'))
                      {
                          if (((TextBox)sender).Text.IndexOf('.') + 2 < ((TextBox)sender).Text.Length)
                          {
                              if (((TextBox)sender).SelectionStart > ((TextBox)sender).Text.IndexOf('.'))
                              {
                                  e.Handled = true;
                              }
                          }
                      }
                  }
                  else if (char.IsControl(e.KeyChar))
                  {
                      // Always OK
                  }
                  else if (e.KeyChar == '.' && !((TextBox)sender).Text.Contains('.'))
                  {
                      // First [.] == OK
                  }
                  else if (e.KeyChar == '-' && !((TextBox)sender).Text.Contains('-'))
                  {
                      // First [-] == OK
                  }
                  else
                  {
                      e.Handled = true;
                  }
              }
          
          
              private void TextBoxAmount_KeyUp(object sender, KeyEventArgs e)
              {
                  if (((TextBox)sender).Text.Contains('-'))
                  {
                      ((TextBox)sender).Text = $"-{((TextBox)sender).Text.Replace("-", string.empty)}";
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 2023-03-14
            • 1970-01-01
            • 2016-09-06
            • 2017-03-08
            • 1970-01-01
            • 2019-10-15
            • 2011-05-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多