/// <summary> /// Patches the events for a specific treeview. /// </summary> /// <param name="treeView">The TreeView.</param> private static void CorrectEventsForTreeView(TreeView treeView) { if (EventsPatched.ContainsKey(treeView)) { throw new InvalidOperationException("Events for this TreeView has been previously patched: '" + treeView.Name + "'"); } EventsPatched.Add(treeView, new Dictionary <string, List <Delegate> >()); foreach (string eventName in EventsToCorrect.Keys) { EventInfo eventInfo = treeView.GetType().GetEvent(eventName); if (eventInfo == null) { throw new InvalidOperationException("Event info for event '" + eventName + "' could not be found"); } EventsPatched[treeView].Add(eventName, new List <Delegate>()); Delegate[] eventDelegates = ContainerHelper.GetEventSubscribers(treeView, eventName); if (eventDelegates != null) { foreach (Delegate del in eventDelegates) { EventsPatched[treeView][eventName].Add(del); eventInfo.RemoveEventHandler(treeView, del); } } eventInfo.AddEventHandler(treeView, EventsToCorrect[eventName]); } }
/// <summary> /// Patches the events for a specific button. /// </summary> /// <param name="btn"></param> private static void CorretEventsForButton(Button btn) { Delegate[] EventDelegates = null; if (EventsPatched.ContainsKey(btn)) { throw new InvalidOperationException("Events for this button has been previously patched: '" + btn.Name + "'"); } EventsPatched.Add(btn, new Dictionary <string, List <Delegate> >()); foreach (string eventName in EventsToCorrect.Keys) { EventInfo eInfo = btn.GetType().GetEvent(eventName); if (eInfo == null) { throw new InvalidOperationException("Event info for event '" + eventName + "' could not be found"); } EventsPatched[btn].Add(eventName, new List <Delegate>()); EventDelegates = ContainerHelper.GetEventSubscribers(btn, eventName); if (EventDelegates != null) { foreach (Delegate del in EventDelegates) { EventsPatched[btn][eventName].Add(del); eInfo.RemoveEventHandler(btn, del); } } eInfo.AddEventHandler(btn, EventsToCorrect[eventName]); } }
/// <summary> /// Remove the event handlers for a control (Disable). /// </summary> /// <param name="ctrl">The control.</param> /// <param name="eventName">The event name.</param> static internal void DisableControlEvents(Control ctrl, string eventName) { Delegate[] _eventDelegates = ContainerHelper.GetEventSubscribers(ctrl, eventName); if (_eventDelegates != null) { EventInfo eventInfo = ctrl.GetType().GetEvent(eventName); if (eventInfo != null) { if (!_eventsDisabled.ContainsKey(ctrl)) { _eventsDisabled.Add(ctrl, new Dictionary <string, List <Delegate> >()); } if (!_eventsDisabled[ctrl].ContainsKey(eventName)) { _eventsDisabled[ctrl].Add(eventName, new List <Delegate>()); } foreach (Delegate del in _eventDelegates) { _eventsDisabled[ctrl][eventName].Add(del); eventInfo.RemoveEventHandler(ctrl, del); } } } }
/// <summary> /// Creates a copy of the baseControl. /// </summary> /// <param name="baseControl">The base control to use as template.</param> /// <returns>A copy or null.</returns> private static Object CloneObject(Object baseControl) { Object result = null; if (baseControl != null) { ICloneable control = baseControl as ICloneable; if (control != null) { result = control.Clone(); } else { result = Activator.CreateInstance(baseControl.GetType()); Dictionary <string, object> listOfProperties = GetPropertiesToCopy(baseControl); foreach (PropertyInfo propertyInfo in baseControl.GetType().GetProperties()) { if (propertyInfo.Name.Equals("Name", StringComparison.CurrentCultureIgnoreCase)) { propertyInfo.SetValue(result, "DinamicallyLoadedControl", null); } else if (listOfProperties.ContainsKey(propertyInfo.Name)) { try { propertyInfo.SetValue(result, listOfProperties[propertyInfo.Name], null); } catch (Exception e) { Trace.TraceError("CloneObject {0}", e.Message); } } } } AddCloneToContainer(baseControl, result); DoExtraBindings(baseControl, result); //Bind the same events set in the base control to the new control foreach (EventInfo eventInfo in baseControl.GetType().GetEvents()) { Delegate[] eventDelegates = ContainerHelper.GetEventSubscribers(baseControl, eventInfo.Name); //The event in the new control will be bound to the same delegates of the base control if (eventDelegates != null) { foreach (Delegate del in eventDelegates) { try { eventInfo.AddEventHandler(result, del); } catch (Exception e) { Trace.TraceError("CloneObject {0}", e.Message); } } } } } return(result); }
/// <summary> /// Cleans the event handlers bound to a control. /// </summary> /// <param name="controlToUnload">Control to clean up.</param> /// <param name="baseObject">Used to get delegates from the control to unload</param> private static void CleanEventHandlers(object controlToUnload, object baseObject) { //Unbind the events set in the control to unload foreach (EventInfo eventInfo in controlToUnload.GetType().GetEvents()) { Delegate[] eventDelegates = ContainerHelper.GetEventSubscribers(baseObject, eventInfo.Name); //The event in the new control will be bound to the same delegates of the base control if (eventDelegates != null) { foreach (Delegate del in eventDelegates) { eventInfo.RemoveEventHandler(controlToUnload, del); } } } }