简体   繁体   English

任何人都能告诉我下面的错误提供者代码之间的差异吗?

[英]Can any one tell me diffrence between below error provider code?

IDE: Visual Studio 2010, C# .net application,winforms IDE:Visual Studio 2010,C#.net应用程序,winforms

I am really surprised that this code Code-1 works fine 我很惊讶这段代码Code-1工作正常

    ErrorProvider ef = new ErrorProvider();
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text == string.Empty)
        {
            ef.SetError(textBox1, "asdf");
        }
        else
        {
            ef.Clear();
        }
    }

but this code Code 2 : 但是这段代码2

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        ErrorProvider ef = new ErrorProvider(); //changes is here
        if (textBox1.Text == string.Empty)
        {
            ef.SetError(textBox1, "asdf");
        }
        else
        {
            ef.Clear();
        }
    }

is not working ie its not providing me error handling facility. 没有工作,即它不提供我错误处理设施。 can anyone tell me the exact reason what is the difference between these two codes. 任何人都可以告诉我这两个代码之间有什么区别的确切原因。 and why the 2nd code is not working fine.. 为什么第二个代码不能正常工作..

code1中,您在事件处理程序范围之外创建ef对象,并且在处理事件之后ef对象仍然存在,其中在code2中ef对象在处理事件后被销毁。

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

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