Miguel

Miguel

  • NA
  • 1
  • 0

Error binding to target method

Jul 18 2009 8:55 PM
private void Record_Click(object sender, EventArgs e) { string[] eventsName = getSelectedEvents(); string[] controlName = getSelectedControls(); for (int i = 0; i < controlName.Length; i++) { Control ctrl = controls[controlName[i]]; foreach (EventInfo eventCtrl in ctrlEvents) { foreach (string eventName in eventsName) { if (eventCtrl.Name == eventName) { Type tDelegate = eventCtrl.EventHandlerType; MethodInfo methodToRegister = typeof(FormRecorder).GetMethod("registerInEventsList", BindingFlags.NonPublic | BindingFlags.Instance); Delegate d = Delegate.CreateDelegate(tDelegate, ctrl, methodToRegister); MethodInfo addHandler = eventCtrl.GetAddMethod(); Object[] addHandlerArgs = { d }; addHandler.Invoke(this, addHandlerArgs); } } } } Form1 form1 = new Form1(); form1.Show(); } ctrl is the control where i'm trying to raise the event, but Delegate d = Delegate.CreateDelegate(tDelegate, ctrl, methodToRegister); gives the following error: "Error binding to target method" if i use "this"(form) instead of the control works fine. Can someone help me with this?