我试图在Windowsform表单中更改errorProvider的图标。我在Properties->Resources文件夹中添加了.ico文件。我尝试过直接复制粘贴.ico文件,还使用了Add resources->Add现有文件选项。无论我做什么,如果我添加我自己的.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;
}
}

例外细节如下:
System.StackOverflowException
HResult=0x800703E9
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>发布于 2021-11-22 09:00:19
通过Resource.resx添加资源也可以很好地引用ico。Copy if new是另一种调用方法。
增加现有资源:

最好将leave事件更改为Validating事件。
我已经测试了您的代码本身,没有问题。
我这里有reproduced the error,因为它是由ico file itself引起的。
它可以通过改变图片来解决.
private void TextBox1_Validating(object sender, CancelEventArgs e) {
if(string.IsNullOrEmpty(textBox1.Text)) {
textBox1.Focus();
errorProvider1.Icon=Properties.Resources.error__3_; //here I have change the default icon
errorProvider1.SetError(textBox1, "Input UserId"); //having exception in this line
} else {
errorProvider1.Icon=Properties.Resources.yes1;
}
}


可以使用文件转换器将其转换为ico。
输出:

发布于 2021-11-22 06:49:50
首先,将您的ico文件添加到资源中,并将“复制到输出目录”设置为始终复制
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text.Trim() == "")
{
errorProvider1.SetError(textBox1, "ERROR WARNING");
errorProvider1.Icon = new Icon(@"Resources\termicon.ico");
}
}https://stackoverflow.com/questions/70061611
复制相似问题