/// <summary> /// Fires the given event on the given element. /// </summary> /// <param name="element">Element to fire the event on</param> /// <param name="eventName">Name of the event to fire</param> public virtual void FireEvent(DispHTMLBaseElement element, string eventName) { // TODO: Passing the eventarguments in a new param of type array. This array // holds 0 or more name/value pairs where the name is a property of the event object // and the value is the value that's assigned to the property. // Execute the JScript to fire the event inside the Browser. string scriptCode = "var newEvt = document.createEventObject();"; scriptCode += "newEvt.button = 1;"; scriptCode += "document.getElementById('" + element.uniqueID + "').fireEvent('" + eventName + "', newEvt);"; try { RunScript(scriptCode); } catch (RunScriptException) { // In a cross domain automation scenario a System.UnauthorizedAccessException // is thrown. The following code works, but setting the event properties // has no effect so that is left out. object dummyEvt = null; // IHTMLEventObj2 mouseDownEvent = (IHTMLEventObj2)parentEvt; // mouseDownEvent.button = 1; object parentEvt = ((IHTMLDocument4)element.document).CreateEventObject(ref dummyEvt); element.FireEvent(eventName, ref parentEvt); } }
/// <summary> /// Fires the given event on the given element. /// </summary> /// <param name="element">Element to fire the event on</param> /// <param name="eventName">Name of the event to fire</param> /// <param name="eventObjectProperties">The event object properties.</param> public static void FireEvent(DispHTMLBaseElement element, string eventName, NameValueCollection eventObjectProperties) { StringBuilder scriptCode = CreateJavaScriptFireEventCode(eventObjectProperties, element, eventName); try { IHTMLWindow2 window = ((IHTMLDocument2)element.document).parentWindow; RunScript(scriptCode.ToString(), window); } catch (RunScriptException) { // In a cross domain automation scenario a System.UnauthorizedAccessException // is thrown. The following code doesn't seem to have any effect, // but maybe someday MicroSoft fixes the issue... so I wrote the code anyway. object dummyEvt = null; object parentEvt = ((IHTMLDocument4)element.document).CreateEventObject(ref dummyEvt); IHTMLEventObj2 eventObj = (IHTMLEventObj2)parentEvt; for (int index = 0; index < eventObjectProperties.Count; index++) { string property = eventObjectProperties.GetKey(index); string value = eventObjectProperties.GetValues(index)[0]; eventObj.setAttribute(property, value, 0); } element.FireEvent(eventName, ref parentEvt); } }
/// <summary> /// Fires the given event on the given element. /// </summary> /// <param name="element">Element to fire the event on</param> /// <param name="eventName">Name of the event to fire</param> /// <param name="eventObjectProperties">The event object properties.</param> public static void FireEvent(DispHTMLBaseElement element, string eventName, NameValueCollection eventObjectProperties) { var scriptCode = CreateJavaScriptFireEventCode(eventObjectProperties, element, eventName); try { var window = ((IHTMLDocument2)element.document).parentWindow; RunScript(scriptCode, window); } catch (RunScriptException) { // In a cross domain automation scenario a System.UnauthorizedAccessException // is thrown. This code does cause the registered client event handlers to be fired // but it does not deliver the event to the control itself. Consequently the // control state may need to be updated directly (eg. as with key press event). object prototypeEvent = null; object eventObj = ((IHTMLDocument4)element.document).CreateEventObject(ref prototypeEvent); for (var index = 0; index < eventObjectProperties.Count; index++) { var property = eventObjectProperties.GetKey(index); var value = eventObjectProperties.GetValues(index)[0]; ((IHTMLEventObj2)eventObj).setAttribute(property, value, 0); } element.FireEvent(eventName, ref eventObj); } }
/// <summary> /// Fires the given event on the given element. /// </summary> /// <param name="element">Element to fire the event on</param> /// <param name="eventName">Name of the event to fire</param> /// <param name="eventObjectProperties">The event object properties.</param> public static void FireEvent(DispHTMLBaseElement element, string eventName, NameValueCollection eventObjectProperties) { StringBuilder scriptCode = CreateJavaScriptFireEventCode(eventObjectProperties, element, eventName); try { IHTMLWindow2 window = ((IHTMLDocument2) element.document).parentWindow; RunScript(scriptCode.ToString(), window); } catch (RunScriptException) { // In a cross domain automation scenario a System.UnauthorizedAccessException // is thrown. The following code doesn't seem to have any effect, // but maybe someday MicroSoft fixes the issue... so I wrote the code anyway. object dummyEvt = null; object parentEvt = ((IHTMLDocument4) element.document).CreateEventObject(ref dummyEvt); IHTMLEventObj2 eventObj = (IHTMLEventObj2) parentEvt; for (int index = 0; index < eventObjectProperties.Count; index++) { string property = eventObjectProperties.GetKey(index); string value = eventObjectProperties.GetValues(index)[0]; eventObj.setAttribute(property, value, 0); } element.FireEvent(eventName, ref parentEvt); } }