0

I made a web browser with vb.Net's webBrowser from the toolbox. When i clicked a button that should works as a navigator (for example, when i clicked button A, the web browser should navigate into a.html, but when i clicked b, the browser should navigate to b.html). The thing is, it doesn't work. Whenever i clicked 1 button, and tried to click other button, the web browser wouldn't navigate.

Here's the code :

Private Sub aToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles aToolStripMenuItem.Click
    browser.Visible = True
    browser.Navigate(New Uri("a.html"))
End Sub

Private Sub bToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles bToolStripMenuItem.Click
    If browser.Visible = True Then
        browser.Navigate(New Uri("\b.html"))
    ElseIf browser.Visible = False Then
        browser.Visible = True
        browser.Navigate(New Uri("\b.html"))
    End If
End Sub

Please correct me if i have any problems in the code. Fyi, i use a menu strip in the project, so the menu strip's choice should work like a button.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Nix
  • 13
  • 4
  • [How to create a Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) – Neo Oct 11 '17 at 13:09
  • Is the \ meant to be part of the b.html uri? a.html doesn't have this and it works fine – Sasha Oct 11 '17 at 13:40

1 Answers1

0

I think your URI formats are not correct. I'm understanding you to mean that both buttons don't work. Let me know if that's not right. If you're looking to read files in the current directory, then from here:

  1. Add the files to your Visual Studio project.
  2. Do a right click->properties on the html files in Visual Studio.
  3. Set the Copy to Output Directory to Copy always.

Then navigate like this:

    Dim curDir As String = System.IO.Directory.GetCurrentDirectory
    browser.Navigate(New Uri(String.Format("file:///{0}/a.html", curDir)))

In this code, the {0} is a parameter, and will be replaced by the value of the curDir variable in the constructor, which will provide the path to the file. The file:/// is the protocol, similar to http://. If you open a regular file in a web browser, such as Chrome (right-click, open with), you'll see this protocol there too.

PerpetualStudent
  • 822
  • 9
  • 17