Mahin
Mahin

Reputation: 27

Changing the icon of ErrorProvider gives exception

I am trying to change the icon of errorProvider in Windowsform. I have added .ico files in Properties->Resources folder. I have tried directly copy pasting the .ico file, have also used the Add resources->Add Existing Files option. What ever I do, if I add my own .ico file and try to set them as errorProvider icon as show in the code below, I get an exception while running the program (particularly when trying to demonstrate the the errorProvider feature in my program). My code is:

 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;
            }
        }

The exception details:

System.StackOverflowException
  HResult=0x800703E9
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

Upvotes: 1

Views: 1086

Answers (3)

Michael Harvey
Michael Harvey

Reputation: 63

enter image description here

enter image description here

double click on Resources.resx in your solution explorer then select resource type and click on Add Resource -> Add Existing file

Upvotes: 0

Jiale Xue - MSFT
Jiale Xue - MSFT

Reputation: 3670

Adding resources through Resource.resx can also refer to ico very well. Copy if new is another calling method.

Add existing resources:

enter image description here

It might be better to change the leave event to the Validating event.

I have tested your code itself and there is no problem.

I have reproduced the error here because it is caused by the ico file itself.

It can be solved by changing a picture.

The error document is here.

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;
    }
}

enter image description here enter image description here

You can use a file converter to convert to ico.

Output:

enter image description here

Upvotes: 4

Buğra Demirtaş
Buğra Demirtaş

Reputation: 94

first add ur ico file to resource and set the "Copy to Output Directory" to copy always

Copy to Outpot Directory Example

 private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        if (textBox1.Text.Trim() == "") 
        {
            errorProvider1.SetError(textBox1, "ERROR WARNING");
            errorProvider1.Icon = new Icon(@"Resources\termicon.ico");
        }
            
        
    
    }

Example

Upvotes: -1

Related Questions