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
Loading HTML without a control
Posted by: Tim Anderson (---.server.ntl.com)
Date: Wednesday, 20-Nov-2002, 09:31:43

The HtmlEditor includes a couple of utility functions that let you load an HTML string into the mshtml DOM without needing to instantiate a control. You might do this if you only need to work with the DOM. Example code:

private void button3_Click(object sender, System.EventArgs e)
{
string somehtml = "<html><body><h1>Test</h1></body></html>";

try
{
mshtml.HTMLDocument doc = new mshtml.HTMLDocument();
onlyconnect.utils.LoadDocument(ref doc,somehtml);

while (doc.readyState != "complete")
{
Application.DoEvents();
}

MessageBox.Show(doc.body.innerHTML);
}

catch (Exception exc)
{
MessageBox.Show(exc.Message);
}

}

Tim

Re: Loading HTML without a control
Posted by: George Soules (---.greenmountainaccess.net)
Date: Wednesday, 23-Apr-2003, 21:23:57

This utility is just what I need, but I'm wondering if DoEvents() is the best way to wait while the document loads.

Doesn't spinning in the loop burn up CPU that would otherwise be used to make the document load more quickly? What I'd really like to do is sleep until the document is loaded.

I tried creating a separate thread in which I load the document and then use Join to wait for the thread to complete, but it appears that the thread may be exiting before readyState changes to complete.

George

Re: Loading HTML without a control
Posted by: Tim Anderson (---.server.ntl.com)
Date: Thursday, 24-Apr-2003, 08:52:06

I would try using events, something like this:

//a couple of class level variables
private mshtml.HTMLDocument doc;
private mshtml.HTMLDocumentEvents2_onreadystatechangeEventHandler myeventhandler;

private void button1_Click(object sender, System.EventArgs e)
{
this.doc = new mshtml.HTMLDocument();
mshtml.HTMLDocumentEvents2_Event ev = (mshtml.HTMLDocumentEvents2_Event) doc;
this.myeventhandler = new mshtml.HTMLDocumentEvents2_onreadystatechangeEventHandler(this.ReadyStateChanged);
ev.onreadystatechange += myeventhandler;
onlyconnect.utils.LoadDocument(ref doc,"<html><head/><body><h1>Hello</h1></body></html>");
//onlyconnect.utils.LoadUrl(ref doc,"http://www.itwriting.com");
}

private void ReadyStateChanged(mshtml.IHTMLEventObj eventobj)
{

if (doc.readyState == "complete")
{
System.Diagnostics.Debug.WriteLine("Doc is loaded");
System.Diagnostics.Debug.Write(onlyconnect.utils.GetDocumentSource(ref doc));
mshtml.HTMLDocumentEvents2_Event ev = (mshtml.HTMLDocumentEvents2_Event) doc;
//clean up
ev.onreadystatechange -= this.myeventhandler;
}
}


--

A snag with this is that if your document contains scripts, the load may fail with a script error. The solution is to disable scripts but I'm not sure how easy this is.

Tim

Re: Loading HTML without a control
Posted by: George Soules (---.greenmountainaccess.net)
Date: Tuesday, 29-Apr-2003, 13:25:53

Using events works well if an asynchronous (non-blocking) call to a method that loads a document is acceptable. However, I have some cases where I need to make synchronous (blocking) calls to the document loading method.

For example, the app calls method X to update the HTML in documents "a", "b", and "c" one at a time in sequence (not in parallel). Method X needs to look something like this:

void hot smiley)
{
UpdateDoc("a");

// "a" is done, ok to do "b"
UpdateDoc("b");

// "b" is done, ok to do "c"
UpdateDoc("c");
}

As such, method UpdateDoc must somehow wait until the onreadystatechange event fires before it can return to X.

A DoEvents spin-wait in UpdateDoc will work, but is not CPU-friendly. Somehow UpdateDoc needs to do a true thread sleep without suspending the code executed as a result of calling LoadDocument.

Since I don't know the magic behind mshtml and don't know who/what is executing between the time LoadDocument is called and the time the onreadystatechange fires, I'm at a loss as how to design the sleep-wait.

I tried putting UpdateDoc in it's own thread, but the thread appeared to exit after calling LoadDocument, but before the onreadystatechange event fired.

If I figure it out, I'll let everyone know how.

Re: Loading HTML without a control
Posted by: Tim Anderson (---.server.ntl.com)
Date: Wednesday, 30-Apr-2003, 20:44:35

I've found most things can be accomplished with onreadystatechange. Loading a number of docs in sequence should be quite feasible. It's a bit like writing a SAX parser, you have to keep track of the state. I agree it's less straightforward than a synchronous call.

Tim

Re: Loading HTML without a control
Posted by: Ho Ngoc Hung (210.245.27.---)
Date: Thursday, 15-May-2003, 10:28:34

I use the HtmlEditor, but the session PrintPreview, it does not work correctly. ex: with the Document has the wisth size is too long compare to A4 paper, show it only display and print a part of it, show how i can print all my document ??



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