2

Hello StackOverflow community,

I am working on a .NET windows application in c# and rebuilt one library project to two, as I want to use some classes in another project.

However since my change I get an error as: cannot convert from 'method group' to 'System.EventHandler', when compiling. From a class / method which has always worked.

The application is a complex mapping application which roles out an xml / xsd mapping with objects, parent and sub relationships, default values, list objects and additional features based on automatically read CRM and Webservice WSDL information.

The following line gives the error: "mappingPickListControls = new MappingPickListControls(pMappingPickList, optionMetadataCollection, ValidationRegister, imglblMandatory, tbControlToValidate_validating);"

The eventhandler is given to the control builders to ensure the method can be called by the correct control to validate the correct information.

CRMMappingPickListForm:

private void tbControlToValidate_validating(object sender, CancelEventArgs e)
    {
        ValidateControl(this, (Control)sender);
    }

public CRMMappingPickListForm(Ciber.Crm.MappingCRMTo.Data.CustomOptionMetadataCollection optionMetaDataCol, Point location, Size size)
    {
        InitializeComponent();
        this.Location = new Point (location.X + (size.Width / 2) - (Size.Width / 2), location.Y + (size.Height / 2) - (Size.Height / 2));
        optionMetadataCollection = optionMetaDataCol;

        ValidationRegister = new FormValidationRegister();
        ValidationRegister.ControlValidationRegister.Add(new ControlValidation("tbConfigurationName", 1, "configuration name", 1));
        mappingPickListControls = new MappingPickListControls(pMappingPickList, optionMetadataCollection, ValidationRegister, imglblMandatory, tbControlToValidate_validating);
    }

MappingPickListControls:

public MappingPickListControls(Panel pMappingPickList, CustomOptionMetadataCollection optionMetaDataCol, FormValidationRegister ValidationRegister, Bitmap imglblMandatory, EventHandler tbControlToValidate_validating) 
    {
        lblPickListValueList = new List<Label>();
        tbPickListMappedValueList = new List<TextBox>();
        foreach (CustomOptionMetadata optionMetaData in optionMetaDataCol)
        {
            AddMapping(pMappingPickList, optionMetaData, ValidationRegister, imglblMandatory, tbControlToValidate_validating);
        }
    }

I got 3 projects in my solution:

MappingCRMTo: Has all the windows forms including the CRMMappingPickListFOrm MappingCRMTo.Controls: Has all the form extensions and form related classes. This is the new project library I created. It also includes MappingPickListControls, which is one of the control builder classes. MappingCRMTo.Data: Has all serialization objects, WSDL reader, zip creator and other classes I like to use throughout some other projects. It is the old location of MappingPickListControls

Kevin Hendricks
  • 785
  • 1
  • 8
  • 36
  • Possible duplicate: http://stackoverflow.com/questions/2479870/how-do-i-fix-compiler-error-cannot-convert-from-method-group-to-system-delega. – meilke Sep 26 '13 at 13:52
  • Seen the post, tried it but my scenario is different as my main question is actually why after this minor change does it not work anymore – Kevin Hendricks Sep 26 '13 at 13:58
  • See below comment. **The thing I don't get is that I did not change the code at all. I just created a new project library and moved the classes, renaming the namespaces. How come this does not work anymore?** – Kevin Hendricks Sep 26 '13 at 13:59

1 Answers1

6

The EventHandler delegate has the following definition:

public delegate void EventHandler(Object sender, EventArgs e)

Your method has the following signature:

private void tbControlToValidate_validating(object sender, CancelEventArgs e)

They aren't an exact match, so you cannot directly assign that method to an EventHandler delegate.

The caller of this method wants to pass in an EventArgs object, but this method expects a CancelEventArgs.

Since you don't actually use those arguments, the simplest option is to just change your method to the following:

private void tbControlToValidate_validating(object sender, EventArgs e)
Servy
  • 202,030
  • 26
  • 332
  • 449
  • That helped thank you. Do you see any reason why I now get this error after moving it to a new project library and did not before? – Kevin Hendricks Sep 26 '13 at 14:17
  • @KevinHendricks It most certainly would have been an error before. You must have changed something relevant to the problem; either you changed the signature, you changed the delegate used, you changed the method you're accessing, or something. There is no way that method would have been convertible to that delegate before. – Servy Sep 26 '13 at 14:19
  • I will take a look at my history once more. Thank you again for the quick response. – Kevin Hendricks Sep 26 '13 at 14:24