【问题标题】:How to check to see if there is a decimal in textbox如何检查文本框中是否有小数点
【发布时间】:2016-03-26 10:58:34
【问题描述】:

我是 C# 新手。我正在制作一个通用应用程序,并试图让用户只能在文本框中输入一位小数。我看到很多人建议使用 KeyPressed 事件,但这对我不起作用。到目前为止,我有以下工作代码:

private void tbTotal_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key < Windows.System.VirtualKey.Number0 || e.Key >= Windows.System.VirtualKey.Number9)
        {
            if (e.Key < Windows.System.VirtualKey.NumberPad0 || e.Key >= Windows.System.VirtualKey.NumberPad9)
            {
                if (e.Key == Windows.System.VirtualKey.Decimal)
                {
                        e.Handled = true;
                }
            }
        }
    }

我怎样才能让它只允许一位小数?先感谢您。

【问题讨论】:

标签: c#


【解决方案1】:
private void tbTotal_KeyDown(object sender, KeyEventArgs e)
    byte a = (byte)e.KeyCode;
    if(e.KeyCode!=Keys.Delete)
    if(a!=8&&a!=13)
    if(a<48||a>57) e.Handled = true;
}

【讨论】:

  • KeyPress 对我不起作用。我收到错误“找不到类型或命名空间名称‘KeyPressEventArgs’。
  • 在页面顶部添加 using System.Windows.Forms;
  • ya 添加使用 System.Windows.Forms;也不能让它工作。我根本无法使用 KeyPress
  • 所以我进行了编辑,因为 Keys.Delete 和 e.KeyCode 给以下错误:Windows.System.VirtualKey.Deletee.Key。我可以输入数字,但不能从数字键盘输入,也不能输入任何小数。
猜你喜欢
  • 2016-01-12
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 2010-12-06
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多