Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据所选类型验证简单文本框的输入_C#_Dynamic_Textbox - Fatal编程技术网

C# 根据所选类型验证简单文本框的输入

C# 根据所选类型验证简单文本框的输入,c#,dynamic,textbox,C#,Dynamic,Textbox,我正在为大一做一些准备,我把自己弄得一团糟,我无法解决。如果你看下图: 您可以看到,我有一个文本框,我不知道如何使它只接受某些字符,例如0-7 sans 8和9代表八进制 以下是我目前的代码: private void inputBox_KeyPress(object sender, KeyPressEventArgs e) { if (hexRadioButton.Checked) { if (char.IsWhiteSpace

我正在为大一做一些准备,我把自己弄得一团糟,我无法解决。如果你看下图:

您可以看到,我有一个文本框,我不知道如何使它只接受某些字符,例如0-7 sans 8和9代表八进制

以下是我目前的代码:

private void inputBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (hexRadioButton.Checked)
        {
            if (char.IsWhiteSpace(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        if (decimalRadioButton.Checked)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ||  char.IsWhiteSpace(e.KeyChar))
            {                    
                e.Handled = true;
            }
        }

        if (octalRadioButton.Checked)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ||  char.IsWhiteSpace(e.KeyChar))
            { 
                e.Handled = true;
            }
            e.Handled = false;
        }

        if (radioButton1.Checked)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
            {
                e.Handled = true;
            }
            e.Handled = false;
        }
    }
我也尝试过这样做:

if (octalRadioButton.Checked)
            {
                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ||  char.IsWhiteSpace(e.KeyChar) || char.IsDigit('7') || char.IsDigit('8'))
var validChars = new[] {'1', '2', '3', '4', '5', '6', '7'};
if (!validChars.Contains(e.KeyChar)) {
    e.Handled = true;
}

但这几乎没有什么作用。

我认为最大的问题在于:

if (octalRadioButton.Checked)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ||  char.IsWhiteSpace(e.KeyChar))
    { 
        e.Handled = true;
    }
    e.Handled = false; <----------
}
您也可以将此方法用于其他基础

编辑:


实际上,我只是看了一下文档,
Handled
显然不起作用。您需要该属性,并在
KeyDown
事件中将其设置为true。

我认为最大的问题在于:

if (octalRadioButton.Checked)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ||  char.IsWhiteSpace(e.KeyChar))
    { 
        e.Handled = true;
    }
    e.Handled = false; <----------
}
您也可以将此方法用于其他基础

编辑:


实际上,我只是看了一下文档,
Handled
显然不起作用。您需要该属性,并在
KeyDown
事件中将其设置为true。

您应该能够结合查找单个类型(dec、hex等)的验证的解决方案-您很可能会找到使用正则表达式验证输入的解决方案。对每一个按键字符进行赋值似乎很混乱。花点时间搜索每种类型的现有解决方案,并相应地应用它们。您应该能够通过查找单个类型(dec、hex等)的验证来组合解决方案-您很可能会找到使用正则表达式验证输入的解决方案。对每一个按键字符进行赋值似乎很混乱。花点时间为每种类型搜索现有的解决方案,并相应地应用它们。