开发者

How to create dynamic control event handler

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"开发者_C百科);


        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...


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

Try this:

    public void Sleep(Object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜