validation of multiple textboxes [closed]

£可爱£侵袭症+ 提交于 2020-01-06 15:16:26

问题


I am creating a C# window application,in which i have taken 10 textboxes.I want to validate each text box means none of the text box should be blank.I have used the errorprovider control for validation on the clicking of submit button. The code is properly working but i want to remove the error providers notifications as soon as i insert the values in blank textboxes.How is it possible Please give me the code through any example.

Thanks in advance.


回答1:


The right way to do this is to handle the Validating event for each TextBox control. There doesn't seem to be any reason to use both Validating and Validated for the scenario you described.

Thus, as Vinay suggests, the best thing to do would be to encapsulate this code into a custom control that inherits from the built-in TextBox. Override the custom control's OnValidating method and place your validation logic that sets/clears the ErrorProvider in there. Then replace each textbox control on your form with an instance of your custom class, instead of the built-in one.

If you really want the validation status to update whenever text is entered in the textbox, you'll need to handle the TextChanged event and call your validation code to set/clear the ErrorProvider. Override the OnTextChanged method in your custom control to do this.




回答2:


Here is the code that I use. You can see there are 2 handlers, one for Validating and second for TextChanged event. DataTextBox shows as an icon in Toolbox so you can place it by mouse and you can set canBeEmpty property also in properties window, default value is true.

public class DataTextBox:TextBox
{
    public DataTextBox()
    {
        this._errorProvider2 = new System.Windows.Forms.ErrorProvider();
        //this.errorProvider1.BlinkRate = 1000;
        this._errorProvider2.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;

        this.TextChanged+=new EventHandler(dtb_TextChanged);
        this.Validating += new System.ComponentModel.CancelEventHandler(this.dtb_Validating);


    }
    private ErrorProvider _errorProvider2;

    private Boolean _canBeEmpty=true;
    public Boolean canBeEmpty
    {
        get { return (_canBeEmpty); }
        set { _canBeEmpty = value; }
    }

    private void dtb_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if ((this.Text.Trim().Length == 0) & !this.canBeEmpty)
        {
            _errorProvider2.SetError(this, "This field cannot be empty.");
            e.Cancel = true;
        }
        else
        {
            _errorProvider2.SetError(this, "");
            e.Cancel = false;
        }
    }

    private void dtb_TextChanged(object sender, EventArgs e)
    {
        if (this.Text.Trim().Length != 0) _errorProvider2.SetError(this, "");
        else _errorProvider2.SetError(this, "This field cannot be empty.");
    }
}

}




回答3:


depending on your implementation, on textbox change event you can call your textbox validated/validating event handler, that will set or clear the error on the basis of whatever logic you have implemented in the validated/validating handler.




回答4:


Typically, validation should happen when focus leaves the textbox - is your validating event being fired here? See example section from this MSDN documentation to see how to use validating and validated events in conjunction to get this right.

As you have many such text-boxes, I will suggest that you create your custom text box control encapsulating validation logic inside.



来源:https://stackoverflow.com/questions/5087844/validation-of-multiple-textboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!