954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Listview? Search? and spcific lines in a text file?

What my question is,

Is it plausible to have a listview item search a specific folder that has nothing but text files in it, pull the first 2 lines from the text file(s) and display them in columns 1 and 2

if so, how would one go about doing it, i've been sitting here for a few hours, search google, msdn, and many of forums trying to get something, i've got absolutly nothing

(Given - Column 1 item, Text file line 1, and text file title will be 3 and the same)

thanx in adv to any and every


ps. i would completely understand if a timer is needed, was mainly going to use this during 'load' and/or after an update to the list of some sort

Smalls
Junior Poster in Training
70 posts since Mar 2008
Reputation Points: 12
Solved Threads: 2
 

Anything is possible.

How are you populating the ListView in the first place?
Can't you also add the information for column 1 and 2 during the population phase?

If the folder is already known, then you can use the information in column 0 and call a function the performs the file reading and retrieving, while you are still populating the ListView.

No timer is needed.

Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

ok, not quite sure i need all these imports!
but,
new objective(s)...
below is what i have so far
line 26
ListView1.Items.Add(New ListViewItem(New String() {SFile.Replace(".\Folder\", ""), CStr(FileLen(SFile))}))
doesn't do exactly what i would like it to do
i'm ok with the filename being in the first column as in the end, the filename, and the first line in the file will be the same(still working with '.txt' files)
what i would like is for the second column to be from the second line of the file

i believe that i could finish all of this if i could just figure out how to create, read, and write .txt files

read and write to specific lines

anyone have anything???
thnx n adv for any n all help

Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class Main

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Things completed on load
        'Clear designer filled objects
        NameLabel.Text = ""
        DescriptionLabel.Text = ""

        'Create working folder if it does not exist, then let the user know
        If My.Computer.FileSystem.DirectoryExists(".\Folder") = True Then
        Else
            MessageBox.Show("Test Failed, Creating Directory!", "Directory Test!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            My.Computer.FileSystem.CreateDirectory(".\Folder")
            If My.Computer.FileSystem.DirectoryExists(".\Folder") = True Then
                MessageBox.Show("Good to go! Directory Now Exists!", "Directory Test!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If

        'Gather a list of items in folder, then remove file path, then display items
        Dim FilesInDir As String() = Directory.GetFiles(".\Folder", "*.txt", SearchOption.AllDirectories)
        Dim SFile As String
        ListView1.Items.Clear()
        For Each SFile In FilesInDir
            ListView1.Items.Add(New ListViewItem(New String() {SFile.Replace(".\Folder\", ""), CStr(FileLen(SFile))}))
        Next

    End Sub

    Private Sub NewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewButton.Click
        'Windows form to add new entry
        My.Forms.NewItem.Show()
    End Sub

    Private Sub EditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditButton.Click
        'Windows form to edit existing entries
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged


        '''I believe that this is the area that i need to fill'''


    End Sub
End Class
Smalls
Junior Poster in Training
70 posts since Mar 2008
Reputation Points: 12
Solved Threads: 2
 

Ok.
You can expand this part of the code to include the marked code I provided:

'Gather a list of items in folder, then remove file path, then display items
Dim FilesInDir As String() = Directory.GetFiles(".\Folder", "*.txt", SearchOption.AllDirectories)
Dim SFile As String

ListView1.Items.Clear()
For Each SFile In FilesInDir
	ListView1.Items.Add(New ListViewItem(New String() {SFile.Replace(".\Folder\", ""), CStr(FileLen(SFile))}))

	'' --------- Add This Code ----------
	Dim stream As New FileStream(".\Folder\" & SFile, FileMode.Open, FileAccess.Read)
	Dim reader As New StreamReader(stream)
	Dim line As String = reader.ReadLine 'This is just to skip the first line in the file

	'Change the number 5 value to match the number of lines to read from the file
	'If only one line is to be read, then remove the For...Next statement but keep what's in it.
	For i As Integer = 1 To 5
		'First check to see if the end of the file is reached
		If Not reader.EndOfStream Then
			'Read the next line from the file
			line = reader.ReadLine
			'Add the line to the SubItems collection of the last added item in the ListView
			ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(line)
		End If
	Next

	reader.Close()
	'' --------- End of Added Code ----------
Next
Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

thnx 4 ur help
i got it finished yesterday mornning

Smalls
Junior Poster in Training
70 posts since Mar 2008
Reputation Points: 12
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: