Results 1 to 5 of 5

Thread: [RESOLVED] Webbrowser control does not load HTML if passed as command line parameter

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    18

    Resolved [RESOLVED] Webbrowser control does not load HTML if passed as command line parameter

    I have created a simple form with a web browser control.

    In the web browser control, I am loading TinyMCE editor .html file.

    No problem, working fine as expected. I can open any HTML file and edit.

    However, problem starts when I try to pass an html file using command line parameter mode.

    It works BUT only when I insert a MessageBox comment before loading the file.

    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            wb.Dock = DockStyle.Fill
            Dim URL As String
            wb.ScriptErrorsSuppressed = True
            Dim AppPath As String = System.AppDomain.CurrentDomain.BaseDirectory()
            AppPath = AppPath.Replace(":", "$")
            URL = "\\127.0.0.1\" & AppPath & "tinymce.html"
            wb.Navigate(URL)
    
            'Handle command line arguments - if supplied
            Dim CmdArg As String() = Environment.GetCommandLineArgs()
            Dim NoOfArgument As Integer = CmdArg.Count
            Select Case NoOfArgument
                Case 1 'No argument supplied
                    'Do nothing
                Case 2 'only filename supplied
                    Dim x As String = Path.GetExtension(CmdArg(1).ToString).ToLower
                    If (x = ".html" Or x = ".htm") Then
                        Dim html As Object = File.ReadAllText(CmdArg(1).ToString)
                        MessageBox.Show(CmdArg(1).ToString, "Loading via command line") 'not working without this!
                        wb.Document.InvokeScript("SetContent", New Object() {html})
                    End If
                Case Else
                    MessageBox.Show("Invalid argument or file" & vbCrLf & "No action taken.", "Argument error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Select
        End Sub
    As you can see in the code, without messagebox line, the form just opens without file loaded in the web browser.
    My WebBrowser control is named as wb

    I have tried to introduced a forced pause using sleep timer but that did not help.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,288

    Re: Webbrowser control does not load HTML if passed as command line parameter

    Try loading it in the Shown event handler instead of the Load event handler.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    18

    Re: Webbrowser control does not load HTML if passed as command line parameter

    Same result, still works only with messagebox

    I also tried with with storing command line argument in a variable and using it in the Form_Shown event but without any luck.

    Interesting part is, if I do not use TinyMCE's script in web browser and simply use vanila web browser control in design mode, then it works fine (even in Form Load event). The puzzle is why web browser is behaving differently if I load the tinymce editor.

    Code:
        Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
            'Handle command line arguments - if supplied
            Dim CmdArg As String() = Environment.GetCommandLineArgs()
            Dim NoOfArgument As Integer = CmdArg.Count
            Select Case NoOfArgument
                Case 1 'No argument supplied
                    'Do nothing
                Case 2 'only filename supplied
                    Dim x As String = Path.GetExtension(CmdArg(1).ToString).ToLower
                    If (x = ".html" Or x = ".htm") Then
                        CurrentlySelectedFile = CmdArg(1).ToString
                        Dim html As Object = File.ReadAllText(CurrentlySelectedFile)
                        MessageBox.Show(CurrentlySelectedFile, "Loading via command line") 'not working without this!
                        wb.Document.InvokeScript("SetContent", New Object() {html})
                        lblStatus.Text = CurrentlySelectedFile
                    End If
                Case Else
                    MessageBox.Show("Invalid argument or file" & vbCrLf & "No action taken.", "Argument error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Select
        End Sub
    Last edited by mobi12; Jan 23rd, 2021 at 07:53 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    18

    Re: Webbrowser control does not load HTML if passed as command line parameter

    Managed to resolve it. Had to introduce a pause manually like this.

    Code:
    Dim html As Object = File.ReadAllText(CurrentlySelectedFile)
    Threading.Thread.Sleep(1000) 'not loading html without a pause
    Application.DoEvents()
    wb.Document.InvokeScript("SetContent", New Object() {html})
    Interesting that when I tried with only sleep timer it did not work, but both sleep and application.doevents did the trick.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,288

    Re: [RESOLVED] Webbrowser control does not load HTML if passed as command line parame

    Try using the DocumentCompleted event of the WebBrowser. You'd have to add some conditional logic so that it only happened the first time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width