简体   繁体   English

文本框的textChanged事件的正则表达式

[英]Regex for textChanged event of TextBox

In my project, There are many TextBoxes inside TabControl to which I am giving same event something like this: ( working ) 在我的项目中,有很多TextBoxes里面TabControl到我赐相同的事件是这样的:( 工作

In my form constructor: 在我的表单构造函数中:

    SetProperty(this);

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

Now I am trying to give TextChanged event to all the TextBoxes. 现在,我尝试将TextChanged事件提供给所有TextBoxes。 something like this: 像这样的东西:

    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?             
    }

I dont know what to write in the above method because there is not one textbox to consider. 我不知道在上面的方法中写什么,因为没有要考虑的文本框。 Please suggest. 请提出建议。

EDIT: The pattern I mentioned shouldn't be allowed into the TextBoxes ie the Text should be converted in to matching string automatically. 编辑:我提到的模式不应该允许进入TextBoxes,即Text应该自动转换为匹配的字符串。 (should disallow the characters I mentioned in the pattern). (应该禁止我在模式中提到的字符)。

You should first grab the reference of the calling TextBox and then you can match the regular expression for validation to take any decision you want. 您应该首先获取调用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
    }
}

ADDED , Better is to hook Validating event, you can call this event anytime you wish to perform Validation for all the TextBoxes at once. 另外 ,更好的方法是挂起Validating事件,您可以在希望一次对所有TextBoxes执行一次Validation的任何时间调用此事件。

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;
}

To perform the validation, call this method: 要执行验证,请调用此方法:

bool isValid = !this.ValidateChildren(ValidationConstraints.Enabled);

References: 参考文献:

  1. Extend the Textbox Control to Validate Against Regular Expressions 扩展文本框控件以针对正则表达式进行验证
  2. Validation in Windows Forms Windows窗体中的验证

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM