HtmlEditor :  Phorum 5 The fastest message board... ever.

This is the discussion forum for the HtmlEditor. See also the HtmlEditor home page, where you can download the control, and the Documentation Wiki, a collaborative project for documenting the control.

Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
Go to Page: 12Next
Current Page: 1 of 2
Calling the onchange of an IHTMLSelectElement
Posted by: Willem Kokke (---.range81-156.btcentralplus.com)
Date: Sunday, 29-Aug-2004, 16:27:03

Hi.

I'm sorry this is not HtmlEditor related, but I've found that this site seems the most comprehensive collection of knowledge about mshtm in dot net.

I'm using the original AxWebBrowser control and so far does everything i need. However, I need to set the value of an HTMLSelectElement. This itself is no problem, i just use the value member. Unfortunately it seems that if one does this programmatically, the onchange event handler never gets called and the associated javascript does not get called.

As a possible solution I want to call the onchange handler myself after setting the value, but i am stumbed as how to do it.

I know how to get the events on a HTMLDocument:

document.onfocusout = this;
c.document.onclick = this;

where 'this' is an object that has the following:

[DispId(0)]
public void DefaultMethod()
{
HTMLWindow2 win = window;
IHTMLEventObj evt =win.@event;
// etc
}

and I assume I can do the same for receiving the events of a HTMLSelectElement. However I want do the reverse operation.

It might go a bit like this

get the {System.__ComObject} in HTMLSelectElement and invoke the DefaultMethod on it and somehow pass in a meaningfull IHTMLEventObj (or equivalent)

but i wouldn't even know where to start.

Also, there is an object HTMLSelectElementEvents, but I cant figure out how to use that either.

So if anybody has any idea I'd be very gratefull!

Thanks,
Willem


Re: Calling the onchange of an IHTMLSelectElement
Posted by: Tim (---.gotadsl.co.uk)
Date: Sunday, 29-Aug-2004, 17:00:41

If you download the example app for HTMLEditor, there's an example of calling a javascript method from your C# code, would that help?

Tim

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Willem Kokke (---.range81-156.btcentralplus.com)
Date: Sunday, 29-Aug-2004, 17:22:23

I've been looking through HtmlEditor and the sample app and I cannot find where it calls a javascript method. i take it you don't mean this:

sb.Append("<body ><h1>Example HTML Document</h1><p onClick=\"alert('Simple script demo');\">Some body text - click to see if scripts are enabled.</p>");

in the sample app?

Anyway, even so, I don't think that would work, since it would need to work on random webpages and i don't know the function name or parameters..

Saying that, I could try to extract the javascript from the outerHTML with a regex or something, that might work, IF I could find a way to execute a javascript string on a page.

So if nothing you've shown me a alternative that looks doable, Thanks alot!!

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Willem Kokke (---.range81-156.btcentralplus.com)
Date: Sunday, 29-Aug-2004, 17:37:06

document.parentWindow.execScript("javascriptstring","JavaScript"); should do that trick, so this seems a viable option.


Still for later reference I'd love to know how to fire an event associated with a single HTMLElement.

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Willem Kokke (---.range81-156.btcentralplus.com)
Date: Sunday, 29-Aug-2004, 19:01:10

I've got this to work but it isn't sufficient.. If an element uses the javascript 'this' operators, it might mean something totally different.

When invoked in an onchange handler it will mean the select box, and when done through HTMLWindow something else :-(

Back to the drawing board

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Tim (---.gotadsl.co.uk)
Date: Sunday, 29-Aug-2004, 19:02:36

In the Demo app, try Execute Script on the Test menu. You have to click the Load HTML button first, otherwise there is no script to execute :-)

The code is:

mshtml.IHTMLWindow2 win = this.htmlEditor1.Document.parentWindow;
win.execScript("runscript();","javascript");

where "runscript" is the name of a Javascript function in the document.

Tim

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Willem Kokke (---.range81-156.btcentralplus.com)
Date: Sunday, 29-Aug-2004, 19:30:01

Duh, must've been blind. Anyway, I figured that out myself, but that still doesn't solve the problem.

The problem is that i don't know what javascript needs to be called. it might be just a simple function, but i might also be a compound statement like this:

<select name="ORIGIN" onChange= "fillSelectFromArray(this.form.DESTINATION, ((this.selectedIndex == 0) ? new Array(new Array('To','-1')) : destination[this.selectedIndex]));" id="from">

<option value="-1" >From</option>
</select>

now in this case extracting the javascript from the outerhtml and calling it wont work. cause they use 'this'

it refers to the select element when used in the onchange handler, but to something totally different when used with execScript() and throws errors.

I'm going to look at calling the onchange handler of DOM element one more time through C# code, and if that doesn't work, i might have to try to insert a javascript function in the HTML DOM at runtime that does

<script type="text/javascript">
function callOnChange()
{
e = document.getElementById('from');
e.onchange();
}
</script>

and then call the function callOnChange through execScript


<shudders at thought>

Thanks,
Willem

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Willem Kokke (---.range81-156.btcentralplus.com)
Date: Sunday, 29-Aug-2004, 20:57:45

Sorry for polluting the forum with offtopic stuff, but I figure other people might want to know the solution as well, and this forum comes up quite alot in google :-)

Anyway, to invoke the eventhandler of an element on a webpage hosted in AxWebbrowser do this:

private void executeOnChangeHandler(IHTMLSelectElement element)
{
&nbsp; if(element.onchange != DBNull.Value)
&nbsp; {
&nbsp; &nbsp; IHTMLElement e = element as IHTMLElement;
&nbsp; &nbsp; IHTMLDocument4 doc = e.document as IHTMLDocument4;
&nbsp; &nbsp; object dummy = null;
&nbsp; &nbsp; object eventObj=doc.CreateEventObject(ref dummy);
&nbsp; &nbsp; HTMLSelectElementClass se = element as HTMLSelectElementClass;
&nbsp; &nbsp; se.FireEvent("onchange",ref eventObj);
&nbsp; }
}

and to format your code you can insert &amp;nbsp; for each tab you want

Re: Calling the onchange of an IHTMLSelectElement
Posted by: dan (---.focaldata.net)
Date: Sunday, 30-Jan-2005, 04:25:41

William thanks for the code, this is exactly what I needed, after 2 days of googling and trying to get the client events to fire this is great. I am new to the browser control and it just seems like all these types of events should be wired up. If you open up IE manually all events fire why should they be different with the control. You should be able to disable the events but I would have thought they would be enabled by default.

Sorry for the ran, thanks again for the code smiling smiley

Re: Calling the onchange of an IHTMLSelectElement
Posted by: James Hancock (---.amtelecom.net)
Date: Wednesday, 09-Feb-2005, 22:12:40

Good stuff here.

I'm working on making it so that javascript events fire automatically no matter what and also raising all of the events properly for all elements so that you can get an element object in my DOM and just subscribe ot it like normal.

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Fred (---.dsl.okcyok.swbell.net)
Date: Wednesday, 09-Mar-2005, 20:15:20

Willem:

Can you cary this one step further -- calling C# function from JavaScript (and passing variables)?

Re: Calling the onchange of an IHTMLSelectElement
Posted by: James Hancock (---.amtelecom.net)
Date: Friday, 11-Mar-2005, 18:41:25

I don't think so, because then you'd be adding to the DOM that javascript sees.

Basically you'd have to create a real ActiveX (.net exposed activex) control to do that.

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Greg (---.lsanca54.covad.net)
Date: Tuesday, 29-Mar-2005, 18:31:30

I am having trouble getting the event under an IHTMLSelectElement to fire as well. I am using Delphi 5. I have used the TWebBrowser component in conjunction with the IHTMLSelectElement.selectedIndex property to set the dropdown to my option. I too am attempting to operate a web page by remote control, so I need the event to fire. I have imported the type library that includes ITHMLDocument4 so that I can have access to the FireEvent method.

My problem is in setting up the arguments. I have a selElement variable that represents the IHTMLSelectElement at hand. I can see the text of the script to fire by doing a ShowMessage(selElement.OnChange):

function anonymous()
{
go_to(this.form);
}

MSDN states that I need three parameters although the code completion feature in Delphi seems to think that only the first two arguments are needed. Those arguments are: 1) a BSTR for the event name, and 2) a VARIANT for an event object of the type IHTMLEventObj.

I am assuming that I can directly use selElement.OnChange to satisfy the BSTR argument. I think my trouble is with the IHTMLEventObj argument. I have declared eveEvent as a variable of the IHTMLEventObj type. Despite my best efforts, I have not been able to figure out how to populate eveEvent.

Can anyone help?


Re: Calling the onchange of an IHTMLSelectElement
Posted by: Oaklahoma (---.superioraccess.net)
Date: Thursday, 09-Jun-2005, 08:24:56

William,

You are a genius! You are my guru!

Is there a really good mshtml book out there that you would recomend?

Thanks,
Oaks

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Irma (168.133.61.---)
Date: Monday, 15-Aug-2005, 19:19:48

Hi everyone,

I am new to MSHTML and wondering what is the difference between IHTMLSelectElement and HTMLSelectElement and also the difference between IHTMLDocument4 and IHTMLDocument2 or IHTMLDocument3?

Thank you in advanced.



Irma

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Ward (---.adsl.cistron.nl)
Date: Tuesday, 16-Aug-2005, 11:18:41

Hi Irma,

IHTMLSelectElement is the interface. HTMLSelectElement is the class that implements that interface.

IHTMLDocument4 and IHTMLDocument2 or IHTMLDocument3 are also interfaces for HTMLDocument, but each with a specific subset of methods. Nowadays in dotnet we would put all those interfaces together in one big interface. In the language the mshtml is programmed in this worked differently as i understand it.

Ward

Re: changefuntion()
Posted by: prasanthi (---.medilandinc.com)
Date: Tuesday, 23-Aug-2005, 15:20:34

function changeLocation() {

location ="#" + document.forms[0].locationNo.options[document.forms[0].locationNo.selectedIndex].value

alert(location);

}




in this function i want to change the seleted location automatically in the same page.i am getting the location number but that particular location is not coming.please give me reply as soon as possible

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Mayank (---.mercy.net)
Date: Wednesday, 14-Sep-2005, 16:26:58

Hi Williem,
I have been following this thread and made good use of it. I was also facing the same problem of unable to fire the onchange event of the Select element. That was quite much possible with the code you posted. Thanks.

I am facing one more problem. I am relying on the documentComplete event of the AXWebBrowser control to do much of much processing. Now this event fires before the javascript's body onload event has fired, and as a result, in the event handler for documentComplete , I am getting unexpected results. I thought of two ways to circumvent this:
1. Obviously, i would like to get the javascript's body onload event to be fired before documentComplete event of the AXWebBrowser control , but thats not possible. So I could move all my code to the javascript's body onload eventhandler (i could trap it in my C# code!!) and start processing from there. The code for this is:

IHTMLWindow2 f = (IHTMLWindow2)(htmldoc.parentWindow);
f.onload = this;

This will call the followinf function for every event:

[DispId(0)]
public void DefaultMethod()
{

HTMLWindow2 win = (HTMLWindow2)((HTMLDocument);

(wbActiveBrowser.wbWebBrowser.Document)).parentWindow;
Console.WriteLine("Object: " + win.@event.srcElement + ", Type: " + win.@event.type);
}


But the issue is, due to this, the actual javascript functions that are there in the document, wont be fired. So we need to execute those functions too. I tried to refire the event by using this code:

IHTMLElement3 f = (IHTMLElement3)win;
object o = win.@event;
f.FireEvent("onload",ref o);

But i am not able to fire the onload event by this. Please help!!!


In essence, i need to setup a hook for the onload event. My C# code should be notified whenever this onload event is fired, and at the same time, the functions defined in javascript for the event, should be executed as well.

Thanks for reading this,
Hoping a reply ...
Mayank

Re: Calling the onchange of an IHTMLSelectElement
Posted by: Mayank (---.mercy.net)
Date: Wednesday, 14-Sep-2005, 18:31:13

Hi Williem,
Can you suggest a way to fire the onload event of the body tag too?
I tried the same way, but it doesnt seem to work.
Mayank

Re: Calling the onchange of an IHTMLSelectElement
Posted by: f1yb0y (207.59.5.---)
Date: Wednesday, 21-Sep-2005, 19:04:36

Willem,

Nicely done!!! Thanks for that. I had to change it into VB, but work fine... I've attached it if anyone else is lookinf for it.

Private Sub ExecuteOnchangeHandler(ByVal element As IHTMLSelectElement)
Dim e As IHTMLElement = element
Dim doc As IHTMLDocument4 = e.document
Dim dummy As Object = Nothing
Dim eventObj As Object = doc.CreateEventObject(dummy)
Dim se As HTMLSelectElementClass = element
se.FireEvent("onchange", eventObj)

End Sub

Regards
F1yb0y

Go to Page: 12Next
Current Page: 1 of 2


Your Name: 
Your Email: 
Subject: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
xWK6F
This is a moderated forum. Your message will remain hidden until it has been approved by a moderator or administrator
This forum powered by Phorum.