Hello guys.

I'am trying to make my search program, so that i can load a text file and search it.

I used this code for browsing and opening

Dim streamer As IO.StreamReader

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        OpenFileDialog1.ShowDialog()
        TextBox1.Text = OpenFileDialog1.FileName
        streamer = IO.File.OpenText(TextBox1.Text)
        Dim mystring() As String = streamer.ReadToEnd.Split(vbNewLine)
        ListBox1.Items.AddRange(mystring)
    End Sub

So this will browse for text file and when I open it, this text file should appear in listbox.

But when I click browse i get a dialog box. If I click cancel this error appears:

System.ArgumentException: Empty path name is not legal.

And one more question how to delete path of my text file. Because when I add it, path appears in textbox.

Recommended Answers

All 6 Replies

you need to check whether the text box contains anything before you attempt to open it. After line 6 put an if statement

if textbox1.text <> "" then

and after line 9

end if

You can check file name length..

Dim streamer As IO.StreamReader
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    OpenFileDialog1.ShowDialog()
    If OpenFileDialog1.FileName.Length > 0 Then
        TextBox1.Text = OpenFileDialog1.FileName
        streamer = IO.File.OpenText(TextBox1.Text)
        Dim mystring() As String = streamer.ReadToEnd.Split(vbNewLine)
        ListBox1.Items.AddRange(mystring)
    End If
End Sub
commented: Thx Jx +1
commented: As always +2

you could set the visible property of the textbox to False or probably more simply just declare a local variable to hold the string

dim FileToOpen as String
FileToOpen = OpenFileDialog1.FileName
commented: You also helped me. Thx +1

you could set the visible property of the textbox to False or probably more simply just declare a local variable to hold the string

dim FileToOpen as String
FileToOpen = OpenFileDialog1.FileName

That textbox is for searching :)

I added at the end of line

TextBox1.Text = ""

Thx guys.

I hope you do not mind the constant questions. I am still a beginner :(

>> I hope you do not mind the constant questions. I am still a beginner
Its okay.. Enjoy Daniweb :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.