-2

I have built my own browser 9see attached code) however I would like to modify the code so that textbox1 reads a txt file at start and uses the contents of that text file to navigate to a URL of equal value to the text with in that text file. All this should happen at the launch of the web browser form.

Example of the text file contents would be http://www.testsite.com

Code as follows:

    #Region "Webbrowser navigation"

    Private Sub Go()
        WebBrowser1.Navigate(TextBox1.Text)

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Go()
    End Sub

    Private Sub TextBox1_keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Go()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.GoBack()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Stop()
    End Sub

#End Region

how can I best do this?

LabRat
  • 1,996
  • 11
  • 56
  • 91

1 Answers1

1

In the event handler for the form's Load event, do something like this:

TextBox1.Text = File.ReadAllText("StartUrl.txt")
Go()

However, unless you have some good reason to use a text file, I would suggest something more flexible and standard such as XML. If you don't mind using the standard app.config file, just add one of those to your project and you can use the ConfigurationManager class to read the setting.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • I'd be happy to help you if you could give me more information. In What did you try? In what way did it not work? – Steven Doggart Jun 05 '12 at 10:19
  • okay I tried to read my txt file, then place that text in the textbox (textbox1) then execute the Go() function. I've done this reading into textbox from txt file before, nothing though seams to apear in the textbox at all? `Private Sub Form1_load() Dim fileText As String = My.Computer.FileSystem.ReadAllText("C:\\test.txt") TextBox1.Text = fileText Go()` – LabRat Jun 05 '12 at 11:19
  • Do you have a try/catch block around that code that may be eating an exception so you never see the error? I'm guessing it's throwing an exception that the specified file doesn't exist. In VB.NET, the back-slash ("\") is not an escape character, so you don't want to put two backslashes in a string if you only want one. It should be "C:\test.txt", not "C:\\test.txt". In other languages, like C#, the double backslash is replaced by a single one, but in VB, it will leave both. – Steven Doggart Jun 05 '12 at 12:08