VeecoTech
VeecoTech

Reputation: 2143

Error on my user control

HI could anyone advice how to solve this error?
I got the response from this thread here but unfortunately there is no more response from the author. So i decided to post here again with his solution.

ERROR: Error 1 Cannot implicitly convert type 'project1.Utility.AdminController.AdminControllerEvent' to 'System.EventHandler'

Error happen when i want to hook the

//btnDelete.Click += new AdminControllerEvent(btnDelete_Click);

namespace project1.Utility
{   
    public partial class AdminController : UserControl
    {
        public delegate void AdminControllerEvent(object sender, AdminControllerEventArgs e);

    public event AdminControllerEvent SaveClick;
    public event AdminControllerEvent DeleteClick;

    public AdminController()
    {
        InitializeComponent();
        //btnDelete.Click += new AdminControllerEvent(btnDelete_Click);
    }

    private void btnDelete_Click(object sender, AdminControllerEventArgs e)
    {
        if (DeleteClick != null)
        {
            if (MessageBox.Show(CoreMessages.DeleteAsk, CoreMessages.DeleteAsk, MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                DeleteClick(sender, e);

                if (AdminControllerEventArgs.Success)
                {
                    MessageBox.Show(CoreMessages.DeleteSuccess, CoreMessages.Successful, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(CoreMessages.DeleteFailed, CoreMessages.Failed, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
    }
}

public class AdminControllerEventArgs : EventArgs
{
    public static bool Success;

    public AdminControllerEventArgs()
        : base()
    {
        Success = true;
    }
}

}


In my Form delete UI

 private void adminController_DeleteClick(object sender, AdminControllerEventArgs e)
    {
        Repository.Delete(user);
    }

Upvotes: 0

Views: 95

Answers (3)

V4Vendetta
V4Vendetta

Reputation: 38230

The problem here is the signature of the method. The button click event has no event data to pass and by convention they have two parameters.

Since it has no event data to pass it uses EventArgs,so since you have made your implementation the Button Click is unaware of that and so the error

Upvotes: 1

k.m
k.m

Reputation: 31484

Button.Click will raise Click event with plain EventArgs arguments. You can't expect Click(object sender, EventArgs e) to work with more specific method signature (using AdminControllerEventArgs arguments). How do you convert EventArgs to AdminControllerEventArgs?

Imagine it worked tho. What happens when:

  • Button.Click creates new EventArgs()
  • Button.Click calls your btnDelete_Click(this, args)
  • args are expected to be of type AdminControllerEventArgs
  • ...but are in fact EventArgs
  • your code in btnDelete_Click tries to access args.Success property
  • ...which isn't there, because EventArgs doesn't have one

Unfortunatelly, you'll need a workaround for this.

Upvotes: 0

Mario
Mario

Reputation: 36567

The event Click expects a handler implementing the signature described by System.EventHandler. That won't work - you have to change the signature or implement your own additional handler for Click that raises another event calling your custom handler. I'm not really sure what you're tryin to do here mixing event handler code with other UI messages etc.

Upvotes: 1

Related Questions