本文介绍了验证多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 C# 窗口应用程序,其中我使用了 10 个文本框.我想验证每个文本框意味着没有一个文本框应该是空白的.我已经使用 errorprovider 控件在单击提交按钮时进行验证.代码工作正常,但我想在我在空白文本框中插入值后立即删除错误提供程序通知.怎么可能请通过任何示例给我代码.

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.

提前致谢.

推荐答案

这是我使用的代码.您可以看到有 2 个处理程序,一个用于 Validating,第二个用于 TextChanged 事件.DataTextBox 在 Toolbox 中显示为图标,因此您可以通过鼠标放置它,您也可以在属性窗口中设置 canBeEmpty 属性,默认值为 true.

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.");
    }
}

}

这篇关于验证多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:53