C# TextBox的textChanged事件的正则表达式

C# TextBox的textChanged事件的正则表达式,c#,.net,regex,winforms,C#,.net,Regex,Winforms,在我的项目中,在TabControl中有许多textbox,我给它们提供了类似这样的事件:(工作) 在我的表单构造函数中: SetProperty(this); 现在,我尝试将TextChanged事件赋予所有文本框。大概是这样的: private void ValidateText(object sender,EventArgs e) { String strpattern = @"^[a-zA-Z][a-zA-Z0-9\'\' ']{1,20}$"

在我的项目中,在
TabControl
中有许多
textbox
,我给它们提供了类似这样的事件:(工作)

在我的表单构造函数中:

    SetProperty(this);

现在,我尝试将TextChanged事件赋予所有文本框。大概是这样的:

    private void ValidateText(object sender,EventArgs e)
    {
        String strpattern = @"^[a-zA-Z][a-zA-Z0-9\'\' ']{1,20}$"; //Pattern is Ok
        Regex regex = new Regex(strpattern);  
        //What should I write here?             
    }
我不知道用上面的方法写什么,因为没有一个文本框要考虑。请建议


编辑:我提到的模式不应该被允许进入文本框,即文本应该自动转换成匹配的字符串。(应该禁止我在模式中提到的字符)。

您应该首先获取调用的
文本框的引用,然后可以匹配正则表达式进行验证,以做出任何决定

private void ValidateText(object sender, EventArgs e)
{
    TextBox txtBox = sender as TextBox;
    String strpattern = @"^[a-zA-Z][a-zA-Z0-9\'\' ']{1,20}$"; //Pattern is Ok
    Regex regex = new Regex(strpattern);
    if (!regex.Match(txtBox.Text).Success)
    {
        // passed
    }
}
添加了,更好的方法是钩住
验证
事件,您可以随时调用此事件,以便同时对所有
文本框
执行验证

private void SetProperty(Control ctr)
{
    foreach (Control control in ctr.Controls)
    {
        if (control is TextBox)
        {
            control.Validating += ValidateText;
        }
        else
        {
            if (control.HasChildren)
            {
                SetProperty(control);  //Recursive function if the control is nested
            }
        }
    }
}

private void ValidateText(object sender, CancelEventArgs e)
{
    TextBox txtBox = sender as TextBox;
    String strpattern = @"^[a-zA-Z][a-zA-Z0-9\'\' ']{1,20}$"; //Pattern is Ok
    Regex regex = new Regex(strpattern);
    //What should I write here?
    if (!regex.Match(txtBox.Text).Success)
    {
        e.Cancel = true;
    }
    e.Cancel = false;
}
要执行验证,请调用此方法:

bool isValid = !this.ValidateChildren(ValidationConstraints.Enabled);
参考资料:


  • 您应该首先获取调用
    TextBox
    的引用,然后匹配正则表达式进行验证,以做出任何您想要的决定

    private void ValidateText(object sender, EventArgs e)
    {
        TextBox txtBox = sender as TextBox;
        String strpattern = @"^[a-zA-Z][a-zA-Z0-9\'\' ']{1,20}$"; //Pattern is Ok
        Regex regex = new Regex(strpattern);
        if (!regex.Match(txtBox.Text).Success)
        {
            // passed
        }
    }
    
    添加了,更好的方法是钩住
    验证
    事件,您可以随时调用此事件,以便同时对所有
    文本框
    执行验证

    private void SetProperty(Control ctr)
    {
        foreach (Control control in ctr.Controls)
        {
            if (control is TextBox)
            {
                control.Validating += ValidateText;
            }
            else
            {
                if (control.HasChildren)
                {
                    SetProperty(control);  //Recursive function if the control is nested
                }
            }
        }
    }
    
    private void ValidateText(object sender, CancelEventArgs e)
    {
        TextBox txtBox = sender as TextBox;
        String strpattern = @"^[a-zA-Z][a-zA-Z0-9\'\' ']{1,20}$"; //Pattern is Ok
        Regex regex = new Regex(strpattern);
        //What should I write here?
        if (!regex.Match(txtBox.Text).Success)
        {
            e.Cancel = true;
        }
        e.Cancel = false;
    }
    
    要执行验证,请调用此方法:

    bool isValid = !this.ValidateChildren(ValidationConstraints.Enabled);
    
    参考资料:


  • 您还可以将textbox控件say this.textbox1发送到事件处理程序方法,并使用事件处理程序中的正则表达式检查该控件的输入文本。

    您还可以将textbox控件say this.textbox1发送到事件处理程序方法,并使用事件处理程序中的正则表达式检查该控件的输入文本处理人。

    谢谢您的回复。。文本框中不应允许使用我提供的模式。。如何做到这一点?在
    Regex.Match
    之前添加
    not
    )猜一猜,如果设置
    CausesValidation
    属性,将进行验证。no。。这意味着我的文本长度范围仅在1到20之间。在顶部评论中,您说“我提供的模式不应被允许进入文本框”??感谢您的回复。。文本框中不应允许使用我提供的模式。。如何做到这一点?在
    Regex.Match
    之前添加
    not
    )猜一猜,如果设置
    CausesValidation
    属性,将进行验证。no。。这意味着我的文本长度范围仅在1到20之间。在顶部评论中,你说“我提供的模式不应该被允许进入文本框”?你担心不同的文本框会有不同的验证模式吗?不。。在这里,我试图给所有我不知道怎么做的东西提供相同的验证模式。你担心不同的文本框会有不同的验证模式吗?不。。在这里,我试图给所有我不知道如何做的验证模式。。