11 Replies - 4037 Views - Last Post: 22 February 2009 - 10:52 AM Rate Topic: -----

#1 weirddemon  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 190
  • Joined: 05-January 09

Working with Text files and Listview

Posted 20 February 2009 - 03:25 PM

Hi,

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

Is This A Good Question/Topic? 0
  • +

Replies To: Working with Text files and Listview

#2 weirddemon  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 190
  • Joined: 05-January 09

Re: Working with Text files and Listview

Posted 21 February 2009 - 05:53 PM

No ideas?
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Working with Text files and Listview

Posted 21 February 2009 - 06:07 PM

Is the ListView.MultiSelect property true or false?
Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Working with Text files and Listview

Posted 21 February 2009 - 06:20 PM

Try something like this:
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:\Users\Dennis\Documents\Development Files\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
		Dim selArray As ListView.SelectedListViewItemCollection = ListView1.SelectedItems
		If selArray.Count > 0 Then
			TextBox1.Text = System.IO.File.ReadAllText(selArray(0).Text)
		End If
	End Sub



This works if the MultiSelect is disabled.
Was This Post Helpful? 1
  • +
  • -

#5 weirddemon  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 190
  • Joined: 05-January 09

Re: Working with Text files and Listview

Posted 21 February 2009 - 09:06 PM

The Multi Text property is selected as True.

When I try your code, Core, with Multi Text property to False, I still get the same error.
Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Working with Text files and Listview

Posted 21 February 2009 - 09:10 PM

The code I showed is working fine in my environment. Please, post the code you are currently using, so we can identify the source of the error.
Was This Post Helpful? 0
  • +
  • -

#7 weirddemon  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 190
  • Joined: 05-January 09

Re: Working with Text files and Listview

Posted 21 February 2009 - 09:38 PM

I guess I haven't actually been able to test if your code is working. I added a button to my main form and it never showed up in debugging mode. This happened before, where the code wouldn't update in debug mode, but I can't remember what I did to fix it. Hmm...
Was This Post Helpful? 0
  • +
  • -

#8 weirddemon  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 190
  • Joined: 05-January 09

Re: Working with Text files and Listview

Posted 22 February 2009 - 10:07 AM

I fixed my problem, and Core, you solution worked like a dream.

I have two more questions...

When the directory doesn't exist, the app locks up because it can't find it. How can I implement an If Exist statement, so that the app doesn't lock up when the directory isn't there?

Also, when I open the form and I click on an item in the list view, close the form and reload it, all of the files have been duplicated. Anyone know why this might be occurring?

This post has been edited by weirddemon: 22 February 2009 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Working with Text files and Listview

Posted 22 February 2009 - 10:15 AM

You can use the Directory.Exists Method to determine if the directory exists before doing anything

If Not System.IO.Directory.Exists("YourDirectory") Then
	  'Let the user know
Else
	  'Do your work here
End If



Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#10 weirddemon  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 190
  • Joined: 05-January 09

Re: Working with Text files and Listview

Posted 22 February 2009 - 10:33 AM

That was perfect, PsychoCoder. Thanks.

I still had the problem with the duplicating lines in the listview.

I think I know why it's happening, but I don't know how to fix it. When the code loads, it tells the program to load all of the text files into the listview, but it never clears the form. So each time the form opens, it puts the lines in there again.
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Working with Text files and Listview

Posted 22 February 2009 - 10:39 AM

In the Form's Load event put

ListView1.Items.Clear()



Before loading anything into it
Was This Post Helpful? 0
  • +
  • -

#12 weirddemon  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 190
  • Joined: 05-January 09

Re: Working with Text files and Listview

Posted 22 February 2009 - 10:52 AM

Thanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1