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开发者_C百科 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);
}
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.
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
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 typeAdminControllerEventArgs
- ...but are in fact
EventArgs
- your code in
btnDelete_Click
tries to accessargs.Success
property - ...which isn't there, because
EventArgs
doesn't have one
Unfortunatelly, you'll need a workaround for this.
精彩评论