I want to be able to grab a specific file from a specified directory, read the lines and display them in a text box. What I want to do, is search for multiple text files in a specified directory, display each name in a Listview and, as I click on them, their info is displayed in the text box below.
I had some help on another forum and this is what I have so far:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myDir As New System.IO.DirectoryInfo("c:\testfolder")
For Each myFile As System.IO.FileInfo In myDir.GetFiles("*.txt")
Dim lvwItem As ListViewItem = ListView1.Items.Add(myDir.ToString & myFile.Name) 'file name
Next
End Sub
Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
TextBox1.Text = System.IO.File.ReadAllText(ListView1.SelectedItems.Item(0).Text)
End Sub
End Class
The problem I'm having is that when I click on a file, it displays the info correctly. But when I click on another file, I get an error:
InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index
It points to the line:
txtLogs.Text = System.IO.File.ReadAllText(ListViewFiles.SelectedItems.Item(0).Text)
Also, when I closed the program and re opened it, it duplicated all of the text files in the list, but did not duplicate them in the directory. Not that I want it to, it just seems odd.
Thanks for any help

New Topic/Question
Reply




MultiQuote




|