Search code examples
c#visual-studio-2012designer

C# Designed doesn't recognize custom event in custom user control


I'm having trouble with VS2012 designer. I have a user control that I designed and in it is a text box (among other things) where the user is meant to enter an IPv4, IPv6, or DNS. I needed to validate that text as valid (TextChanged event) report back to the main program. Consider the following code:

private bool addressError;

public EventHandler ErrorChanged;

public bool Error
{
    get
    {
       return addressError;
    }
    set
    {
        if (this.Error != value)
        { 
            addressError = value;
            OnErrorChanged(this, EventArgs.Empty);
        }
    }
}

protected virtual void OnErrorChanged(object sender, EventArgs e)
{
    if (ErrorChanged != null)
    {
        ErrorChanged(sender, e);
    }
}

Then I add the event handler to the main program designer (control name is "Com"):

this.Com.ErrorChanged += new System.EventHandler(this.Com_ErrorChanged);

The problem I have is that although the code works exactly as I wanted it to, the designer thinks there is no ErrorChanged event. The exact message it reports is

"The type 'ModbusCom.Communications' has no event named 'ErrorChanged'."

I can ignore the error and the designer displays the form ok. I can run the program and everything is fine, but it's a bit annoying to have to keep telling it to ignore the problem. Is there anything I can do to resolve this? Help is appreciated!


Solution

  • You aren't declaring it as an event

    Change:

    public EventHandler ErrorChanged;
    

    into:

    public event EventHandler ErrorChanged;