C#根据复选框限制和分类文本框输入

C# Limiting and classifying Text-Box input according to Check-Boxes

Here 我有三个复选框和一个文本框,我想教我的应用程序一些字符,因此我需要将文本框中的字符输入分为三个 类。文本框应该只包含一个字符(已解决),在输入字符后,我通过选中三个复选框之一对其进行分类。如果输入数字我勾选 "DIGIT" 复选框,如果输入字母(lower/upper 大小写)我勾选 "UPPERCASE" 复选框,如果是符号,我勾选 "AllSymbols" 复选框. 如果我输入数字并检查大写或 AllSymbols 而不是 "DIGIT" 复选框,它应该在按确定后抛出错误通知(已解决)。对于另外两个 类,我需要相同的逻辑。

public void GetParameters(out string charCode, out int classes)
{
    charCode = textBoxCharCode.Text;

    try
    {
        if (checkBoxDigit.Checked) Convert.ToInt16(textBoxCharCode.Text);
    }

    catch
    {
        MessageBox.Show("You should enter Digits", "Error", MessageBoxButtons.OK);
    }

    if (charCode.Length != 1) MessageBox.Show("You should enter only One character", "Error", MessageBoxButtons.OK);

    classes = (checkBoxDigit.Checked ? (int)EOCRClass.Digit : 0) | (checkBoxUpperCase.Checked ? (int)EOCRClass.UpperCase : 0) | (checkBoxAllSymbols.Checked ? (int)EOCRClass.LowerCase : 0);
}

有什么建议吗?

创建 3 个正则表达式。一种用于数字(数字)(class 0),一种用于大写(class 1),一种用于所有符号(class 2)。 在 Ok button click event 中,选中复选框 checked (class 0 or 1 or 2 ) 并根据适当的正则表达式验证文本框中的输入。 如果验证失败,则抛出错误通知。