我正在嘗試更改 Windowsform 中 errorProvider 的圖示。我在 Properties->Resources 檔案夾中添加了 .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;
}
}

例外詳情:
System.StackOverflowException
HResult=0x800703E9
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
uj5u.com熱心網友回復:
通過添加資源Resource.resx也可以很好的參考ico。Copy if new是另一種呼叫方法。
添加現有資源:

將leave事件更改為事件可能會更好Validating。
我已經測驗了你的代碼本身,沒有問題。
我在reproduced the error這里是因為它是由ico file itself.
換一張圖就可以解決。

您可以使用檔案轉換器轉換為 ico。
輸出:

uj5u.com熱心網友回復:
首先將您的 ico 檔案添加到資源并將“復制到輸出目錄”設定為始終復制
復制到 Outpot 目錄示例
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://www.uj5u.com/qita/362764.html
