【问题标题】:how to make a text box only to accept numbers in c# for windows web store application using blank template [closed]如何使用空白模板使文本框仅接受 c# 中的数字用于 Windows 网络商店应用程序 [关闭]
【发布时间】:2013-05-02 02:10:47
【问题描述】:

我正在 Visual Studio 12 中创建一个 Windows 商店应用程序,我使用的是 c# 语言,我有一个文本框,但是如何让它只接受数字,如果用户尝试输入任何其他值而不是它应该输入的数字显示错误信息

【问题讨论】:

标签: c# windows textbox webstore


【解决方案1】:

除了其他答案之外,当您正在编写 Windows 应用商店应用程序并且很可能会处理虚拟键盘时,您可以通过设置 @ 的 InputScope 来确保获得合适的键盘视图987654324@正确(MSDN link here

<TextBox InputScope="Number" .../>

有一堆有用的InputScopedescribed here

请注意,您仍然需要按照其他答案中的说明进行验证,因为您必须满足用户覆盖显示的键盘类型或连接物理键盘的需求。我会用KeyDown 事件处理程序来做,就像这样

private void TextBox_KeyDown_Number(object sender, KeyRoutedEventArgs e)
{
    if ((uint)e.Key >= (uint)Windows.System.VirtualKey.Number0 
        && (uint)e.Key <= (uint)Windows.System.VirtualKey.Number9)
    {
        e.Handled = false;
    }
    else e.Handled = true;       
}

【讨论】:

  • 组合键,如 Shift+1 或 Shift+任何允许的数字键,打印 !@# 字符。
【解决方案2】:

您可以简单地使用trycatch,如下例所示:

private void textBox1_TextChanged(object sender, EventArgs e)
    {

        int num;

        try
        {
            num = int.Parse(textBox1.Text);  //here's your value
            label1.Text = num.ToString();
        }

        catch (Exception exc)
        {
            label2.Text = exc.Message;
        }
    }

【讨论】:

    【解决方案3】:

    您可以使用 trycatch

    或者您可以通过执行此操作获取更多代码来确定输入是否为数字(int 或 double)

    //---------------------------------------------------------------------------
    bool TFmBatteryConfiguration::IsValidInt(char* x)
    {
        bool Checked = true;
    
        int i = 0;
        do
        {
            //valid digit?
            if (isdigit(x[i]))
            {
                //to the next character
                i++;
                Checked = true;
            }
            else
            {
                //to the next character
                i++;
                Checked = false;
                break;
            }
        } while (x[i] != '\0');
    
        return Checked;
    }
    
    //---------------------------------------------------------------------------
    bool TFmBatteryConfiguration::IsValidDouble(char* x)
    {
        bool Checked = true;
    
        int i = 0;
    
        do
        {
            //valid digit?
            if (isdigit(x[i]))
            {
                //to the next character 
                i++;
                Checked = true;
            }
            else if (x[i] == '.')
            {
                //First character
                if (x[0] == '.')
                {
                    Checked = false;
                    break;    
                }
                else if (x[i] == '.' && x[i+1] == '\0')
                {
                    Checked = false;
                    break;
                }
                else
                {
                    //to the next character
                    i++;
                }
            }
            else
            {
                i++;
                Checked = false;
                break;
            }
        } while (x[i] != '\0');
    
        return Checked;
    }
    

    上面的代码直接取自我的一个 C++ 项目。但想法是一样的。 C# 提供有 char.isDigit()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2012-10-08
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多