更改 ErrorProvider 的图标会出现异常

Mah*_*hin 1 c# errorprovider windows-forms-designer visual-studio .net-4.8

我正在尝试更改 Windowsform 中 errorProvider 的图标。我已在“属性”->“资源”文件夹中添加了 .ico 文件。我尝试过直接复制粘贴 .ico 文件,还使用了“添加资源”->“添加现有文件”选项。无论我做什么,如果我添加自己的 .ico 文件并尝试将它们设置为 errorProvider 图标(如下面的代码所示),我会在运行程序时遇到异常(特别是在尝试演示程序中的 errorProvider 功能时) 。我的代码是:

 private void textBox1_Leave(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textBox1.Text))
            {
                textBox1.Focus();
                errorProvider1.Icon = Properties.Resources.cross; //here I have change the default icon
                errorProvider1.SetError(this.textBox1, "Input UserId"); //having exception in this line
            }
            else
            {
                errorProvider1.Icon = Properties.Resources.right;
            }
        }
Run Code Online (Sandbox Code Playgroud)

异常详细信息:

System.StackOverflowException
  HResult=0x800703E9
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>
Run Code Online (Sandbox Code Playgroud)

Jia*_*SFT 6

通过添加资源Resource.resx也可以很好的参考ico。Copy if new是另一种调用方法。

\n

添加现有资源\xef\xbc\x9a

\n

在此输入图像描述

\n

leave把事件改成事件可能会更好Validating。

\n

我已经测试了你的代码本身没有问题。

\n

我reproduced the error这里是因为它是由ico file itself.

\n

可以通过换一张图片来解决。

\n

错误文档在这里。

\n
private void TextBox1_Validating(object sender, CancelEventArgs e) {\n    if(string.IsNullOrEmpty(textBox1.Text)) {\n            textBox1.Focus();\n            errorProvider1.Icon=Properties.Resources.error__3_; //here I have change the default icon\n            errorProvider1.SetError(textBox1, "Input UserId"); //having exception in this line\n        } else {\n            errorProvider1.Icon=Properties.Resources.yes1;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述\n在此输入图像描述

\n

您可以使用文件转换器将其转换为 ico。

\n

输出:

\n

在此输入图像描述

\n