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.

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

Edited 4 Years Ago by Jx_Man: n/a

Comments
As always
Thx Jx

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
Comments
You also helped me. Thx

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 :)

This question has already been answered. Start a new discussion instead.