Home > Mobile >  No overload matches delegate when creating custom event for a WinForm application
No overload matches delegate when creating custom event for a WinForm application

Time:05-03

I am learning to make a WinForm with c# and I am new to the concept of delegate. After some research on the Internet, it seems that this error arises from inconsistent between the delegate and EventHandler. but they seem fine to me, at least EventArgs definitions are the same. Please tell me what goes wrong.

I've placed this inside the Form1 constructor:

ImageRenamed  = OnImageRenamed;

The following code is the custom event I would like to raise when applyBtn_Click is fired.

public delegate void ImageRenamedEventHandler(object sender, EventArgs e);

public event ImageRenamedEventHandler ImageRenamed;

protected virtual void OnImageRenamed()
{
    if (ImageRenamed != null)
        ImageRenamed(this, EventArgs.Empty);
}

private void applyBtn_Click(object sender, EventArgs e)
{
    // do something
    OnImageRenamed();
}

CodePudding user response:

You don't need a custom delegate if you're seeking attachment of a method with an (object,EventArgs) signature, just use

public event EventHandler ImageRenamed;

and do away with that delegate definition

Also, you can look at changing the event invoke

protected virtual void OnImageRenamed(EventArgs e)
{
    EventHandler eh = ImageRenamed;
    eh?.Invoke(this, e);
}

(And call that with EventArgs.Empty)

Caching it into a local variable thus could help prevent potential issues if the handler is removed in the small window of time between using if to determine if it should be invoked and actually invoking it

If the same event will be raised for multiple different images being renamed, consider providing an EventArgs that informs the receiver which image was renamed.


I've placed this inside the Form1 constructor:

Also worth pointing out you seem to have gone wonky here. The OnImageRenamed method invokes the event and is called by the code originating the event. Classes that raise events may do so via a OnXxx method (but they don't have to)

It's not intended to be the method that handles the event. You'd do something more like:

this.ImageRenamed  = <press the TAB key twice>

and VS would do something like this for you:

  this.ImageRenamed = Form_ImageRenamed;

} 

private void Form_ImageRenamed(object sender, EventArgs e){
  throw new NotImplementedException();
}

You replace that NIE with the code you want to run when an image is renamed

Also worth noting that you don't really need the form to handle its own event, because it knows it's occurring. It's more typical to have something like an instance of a button (a self contained class) that you use on your form and attach one of your form's methods to its click event; the method that handles the event (in your form) is thus external to the class raising it (Microsoft's button). You could perhaps have another form that has the instance of the form that does the renaming and do e.g.:

renamerForm.ImageRenamed  = ...

this way when renaming occurs in foreign form renamerForm a method in the local form is invoked. The sender will be renamerForm


Internally, consider an event to be little more than a list of methods (all with the same signature) that are invoked in any order when the event is raised. You append some method to the list with = and it will be accepted as long as it's signature matches. The following is also valid:

ImageRenamed  = (s,e) => MessageBos.Show("The image was renamed");

CodePudding user response:

You have almost done. Your OnImageRenamed needs some parameters:

    protected virtual void OnImageRenamed(object sender, EventArgs e)
    {
        if (ImageRenamed != null)
            ImageRenamed(this, EventArgs.Empty);
    }
  • Related