• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Order of form initialization

I'm using the Microsoft Web Browser control and the thing's driving me nuts.  Specifically, I'm trying to assign text to the document object before the form shows on screen.  Here's the form load:

    Public Doc As mshtml.HTMLDocumentClass
    Public txtMessage as String

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        AxWebBrowser1.Navigate2("about:blank") ' Supposedly creates a document object
        Doc = AxWebBrowser1.Document ' Loads the browser
        Doc.designMode = "On" ' Sets to design

        If Not txtMessage Is Nothing Then
            If txtMessage.Trim <> "" Then
                Doc.body.innerHTML = txtMessage.Trim ' Error - doc = nothing
        ' etc...
    End Sub

'doc' is always 'Nothing'.  My problem is finding a point in the form processing where doc = something.  If I don't assign a value to txtMessage.Text, the browser (editor) loads no problem and doc = something.

I've tried adding a Page_Load but it's never executed.  Cause it's been overridden above?

So what comes after OnLoad in the order of form initialization?  In which event can I assign this value where doc = something?  (if you ask me it should already be something at this point)

Thanks!
0
MIKEV
Asked:
MIKEV
  • 6
  • 6
  • 2
2 Solutions
 
S-TwilleyCommented:
you need to give the browser control time to create the document object...  a good time to do this is after the first time the DocumentComplete event is called (i.e. for the Navigate("about:blank")  call).

what specifically are you planning to do by inserting your own HTML into the webbrowser control... you can get the document model without using the browser control...  and check things

    Dim firstCall As Boolean = True

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        If firstCall Then
            InsertMyCode()
            firstCall = True
        Else

        End If
    End Sub

    Sub InsertMyCode()
        Dim thisDoc As mshtml.ihtmldocument2
        thisDoc = AxWebBrowser1.Document
        thisDoc.body.innerHTML = "BLAH <b>BLAH</b>"
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AxWebBrowser1.Navigate2("about:blank") ' Supposedly creates a document object
    End Sub
0
 
S-TwilleyCommented:
sorry, this should read..

        If firstCall Then
            InsertMyCode()
            firstCall = False
        Else
0
 
S-TwilleyCommented:
and later on...    when you want to write to the document.body object, just check if   firstCall = False ... if so, then it's ok to write to
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Erick37Commented:
The document must fully load before you can use it.  I'm not familar with the HTMLDocumentClass object, but you can use Private Doc As HTMLDocument , and load it up like this:

        AxWebBrowser1.Navigate2("about:blank") ' Supposedly creates a document object
        Do While AxWebBrowser1.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE
            Application.DoEvents()
        Loop
        Doc = AxWebBrowser1.Document ' Loads the browser
0
 
Erick37Commented:
Way too late...
0
 
MIKEVAuthor Commented:
Worked perfectly, thanks!  I'll shoot Erik37 a few, was equally correct.
0
 
MIKEVAuthor Commented:
Well, actually, there's one more detail that's hanging me up.

The browser needs to be in edit mode.

    Sub InsertMyCode()
        Dim thisDoc As mshtml.IHTMLDocument2
        thisDoc = AxWebBrowser1.Document
        If Not txtMessage Is Nothing Or txtMessage.Trim.Trim <> "" Then
            thisDoc.body.innerHTML = txtMessage.Trim
        Else
            thisDoc.execCommand("FontName", False, "Arial")
            AxWebBrowser1.Focus()
            thisDoc.focus()
        End If
        thisDoc.designMode = "On" ' Sets to design
    End Sub

Problem is, as long as the designmode line is anywhere in this sub, the web browser is blank, even though I see the code assign a huge HTML string to it.  Any thoughts?  Other than the Edit portion it works well.

Thanks!
0
 
S-TwilleyCommented:
what are you setting the design mode for?
0
 
MIKEVAuthor Commented:
The browser is being used as and HTML editor.
0
 
S-TwilleyCommented:
Im just wondering if you're setting it to design mode too soon... just for debug purposes, take the designmode line out, and adda button to your form and put that line in.. allow your doc to load in the text... and once you can see it's done, click on the button and see if it clears out the webbrowser
0
 
MIKEVAuthor Commented:
Good idea...  The browser appears with the HTML I assigned, non editable.  I click the button that sets

thisDoc.DesignMode = "On"

and the browser window clears completely.  In my original example:

        AxWebBrowser1.Navigate2("about:blank") ' Supposedly creates a document object
        Doc = AxWebBrowser1.Document ' Loads the browser
        Doc.designMode = "On" ' Sets to design

This worked, it was just the initialization of the body that was giving me problems.  I've tried this:

    Sub InsertMyCode()
        thisDoc = AxWebBrowser1.Document
        thisDoc.designMode = "On" ' Sets to design
        If Not txtMessage Is Nothing Or txtMessage.Trim.Trim <> "" Then

...right after the creation of the document but although it is in edit mode, the window is blank.

0
 
MIKEVAuthor Commented:
Actually, don't worry about it.  I've had it with this control, it's been nothing but a complete horror show, like most of MS's controls.  I'll bite the bullet and buy the tx Text Control product from textcontrol.com.  It's made for stuff like this and much more.

...but thank you for the assistance, it's very much appreciated.
0
 
S-TwilleyCommented:
No problem.... and i know how you feel, the WebBrowser control, as well as the InternetExplorer object for hooking onto IE... have proved problematic for me, especially with the documentcomplete event
0
 
MIKEVAuthor Commented:
Cool, I'm not insane.  Thanks! :)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now