1

I have problem with control event handler. I created a control ( button ) and I want to bind click event with a method. But I have "Error binding to target method." exception.

code is,

class SetControlEvent
{

    public void Sleep()
    {
        System.Threading.Thread.Sleep(5000);
    }

    internal void Set(object theObject,XmlNode theControlNode)
    {
        EventInfo ei = theObject.GetType().GetEvent("Click");


        EventDescriptorCollection events = TypeDescriptor.GetEvents(theObject);
        foreach (EventDescriptor theEvent in events)
        {
            foreach (XmlAttribute attribute in theControlNode.Attributes)
            {
                if (theEvent.DisplayName == attribute.Name)
                {

                    MethodInfo mi = typeof(SetControlEvent).GetMethod("Sleep");
                    Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, this, "Sleep");

                    theEvent.AddEventHandler(theObject, del);



                    break;

                }
            }
        }
    }

}

so, what should I do?

thanks...

1 Answer 1

1

Your Sleep method signature isn't adequate to EventHandler delegate.

Try this:

    public void Sleep(Object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.