如何验证 Windows 表单中特定模式的文本框 c#

how to validate a textbox for specific pattern in Windows form c#

我想针对仅包含数字、点和加号的特定输入模式验证文本框。 例如。

50.4+50.6+60.7+80.4 等...

我希望用户只能在此模式中输入,因为最后我想加上所有由 plus singh 分隔的值。所以用户有必要遵循这种模式。

请任何人给我解决方案。 我正在使用 c# Windows 表单应用程序。

使用按键事件:

private void CheckInput(object sender, KeyPressEventArgs e)
{
    // Make sure only digits, . and + 
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '+')
    {
        e.Handled = true;
    }
    // Make sure . is in correct places only
    else if (e.KeyChar == '.')
    {
        for (int i = textBox1.SelectionStart - 1; i >= 0; i--)
        {
            if (textBox1.Text[i] == '.')
            {                       
                e.Handled = true;
                break;
            }
            else if (textBox1.Text[i] == '+') break;
        }
    }
    // Make sure character before + is a digit
    else if (e.KeyChar == '+' 
        && !char.IsDigit(textBox1.Text[textBox1.SelectionStart - 1]))
    {
        e.Handled = true;
    }
}