Home > database >  Changing the icon of ErrorProvider gives exception
Changing the icon of ErrorProvider gives exception

Time:11-22

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>

CodePudding user response:

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.

enter image description here enter image description here

You can use a file converter to convert to ico.

Output:

enter image description here

CodePudding user response:

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

  • Related