12 Replies - 428 Views - Last Post: 16 July 2014 - 02:53 AM Rate Topic: -----

#1 HappyMan20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-July 14

Trying to extract listbox text files in to textbox

Posted 13 July 2014 - 08:43 PM

I want the program to extract the text files from a folder based on whether the text files contain "@" anywhere. Then when selecting through the list in listbox have it show what each text file looks like in a multiline text box. I have been experimenting. I am able to list all the text files into the listbox however even that shows the directory (C:\Users\Desktop). I was trying to have the listbox only show the title of the text file. Also, I was trying to have it read through the text files looking for "@" this is what I am having trouble with. Also these text files I have are only a few lines. After that I would click on a file in the listbox and then it would show the text of the specific text file in the multiline text box.



Dim dialog As New FolderBrowserDialog
        If DialogResult.OK = dialog.ShowDialog Then
            TextBox1.Text = dialog.SelectedPath
        End If

        Dim fileNames = My.Computer.FileSystem.GetFiles(
            TextBox1.Text, FileIO.SearchOption.SearchAllSubDirectories, "*.txt")

        For Each fileName As String In fileNames
            Dim objReader As New System.IO.StreamReader(fileName)
            Do While objReader.Peek() <> -1
                objReader.ReadToEnd()
                If fileName.Contains("@") Then
                    ListBox1.Items.Add(fileName)
                End If
            Loop

        Next

        TextBox2.Text = ListBox1.SelectedItem


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to extract listbox text files in to textbox

#2 Kizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-July 14

Re: Trying to extract listbox text files in to textbox

Posted 14 July 2014 - 06:51 AM

To have just the title of the file then after
If fileName.Contains("@") Then


Add:
Dim fileStr = Path.GetFileName(fileName)
                    ListBox1.Items.Add(fileStr)

Was This Post Helpful? 0
  • +
  • -

#3 Kizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-July 14

Re: Trying to extract listbox text files in to textbox

Posted 14 July 2014 - 07:40 AM

I have come up with part of the solution for this, the below code will work, however if the file is in a sub folder, for example the folder chosen in the folder browser dialog is the desktop, however the file is located in a folder on the desktop then when the user try's to open this folder an exception is thrown, so you may have to look into this

Imports System.IO

Public Class Form1

    Dim filePath As String
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim folderDialog As New FolderBrowserDialog

        If DialogResult.OK = folderDialog.ShowDialog Then
            filePath = folderDialog.SelectedPath
            TextBox1.Text = filePath
        End If

        Dim files = Directory.GetFiles(TextBox1.Text, "*.txt", SearchOption.AllDirectories)

        For Each file As String In files
            Dim objReader As New System.IO.StreamReader(file)

            Do While objReader.Peek() <> -1
                objReader.ReadToEnd()
                If file.Contains("@") Then
                    Dim fileStr = Path.GetFileName(file)

                    ListBox1.Items.Add(fileStr)
                End If
            Loop
        Next
    End Sub
    Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
        Dim selectedItem = ListBox1.SelectedItem

        Dim file As String = filePath & "\" & selectedItem.ToString
        Dim reader As New System.IO.StreamReader(file)
        TextBox2.Text = reader.ReadToEnd
    End Sub

End Class



If you need any further help please let me know, and let me know how it goes
Was This Post Helpful? 0
  • +
  • -

#4 HappyMan20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-July 14

Re: Trying to extract listbox text files in to textbox

Posted 14 July 2014 - 04:07 PM

View PostKizzle, on 14 July 2014 - 07:40 AM, said:

I have come up with part of the solution for this, the below code will work, however if the file is in a sub folder, for example the folder chosen in the folder browser dialog is the desktop, however the file is located in a folder on the desktop then when the user try's to open this folder an exception is thrown, so you may have to look into this
If you need any further help please let me know, and let me know how it goes


I ran the code you gave me however nothing shows up in the listbox after I selected the folder. The code I used searched all subdirectories.
Was This Post Helpful? 0
  • +
  • -

#5 HappyMan20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-July 14

Re: Trying to extract listbox text files in to textbox

Posted 14 July 2014 - 04:48 PM

It might be a problem with my streamreader. I got rid of the streamreader part and then the listbox showed the text files names (not the directory anymore I used part of your code). On the last part when the text box will read the text files; using your code that part did not work for me.
Was This Post Helpful? 0
  • +
  • -

#6 Kizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-July 14

Re: Trying to extract listbox text files in to textbox

Posted 15 July 2014 - 01:13 AM

Which part did not work? is it giving any errors?
Was This Post Helpful? 0
  • +
  • -

#7 HappyMan20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-July 14

Re: Trying to extract listbox text files in to textbox

Posted 15 July 2014 - 11:39 AM

View PostKizzle, on 15 July 2014 - 01:13 AM, said:

Which part did not work? is it giving any errors?


I didn't get any errors, and nothing showed up in the listbox when I used your code. Im not really sure how long it should take though. Ill try again using your code and ill leave it open for a few minutes.
Was This Post Helpful? 0
  • +
  • -

#8 HappyMan20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-July 14

Re: Trying to extract listbox text files in to textbox

Posted 15 July 2014 - 11:55 AM

Ok I left it open for a few minutes and nothing happened. I copied over your code and checked the format that what I ran. I then replaced your code
Dim files = Directory.GetFiles(TextBox1.Text, "*.txt", SearchOption.AllDirectories)



with mine

Dim files = My.Computer.FileSystem.GetFiles(
                    TextBox1.Text, FileIO.SearchOption.SearchAllSubDirectories, "*.txt")


That didn't work either still nothing in listbox. Like I said without the streamreader part; I get files in the listbox. I am using visual basic 2010.
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 221
  • View blog
  • Posts: 735
  • Joined: 01-February 13

Re: Trying to extract listbox text files in to textbox

Posted 15 July 2014 - 04:07 PM

Hi,
Below is an example of what i believe you are trying to do. In order to show just the File Name in the listbox you will need to keep a list of all the full path names in the directory so that you can access the full path name from the list by using the SelectedIndex of the Listbox when you select a file in it.

I tried to comment the code a little so you can try to follow what is being done but, you may want to look through the links below to get a better understanding of some of the methods and Classes i used in it.

File.ReadAllText Method (String)
List(Of T) Class

Public Class Form1
    Private FileList As New List(Of String) 'The list that will hold the full path and name to each file

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using fb As New FolderBrowserDialog
            If fb.ShowDialog = DialogResult.OK Then

                ListBox1.Items.Clear() 'Clear all items from the ListBox
                FileList.Clear() 'Clear all file pathnames from the FileList

                TextBox1.Text = fb.SelectedPath

                For Each f As String In IO.Directory.GetFiles(fb.SelectedPath, "*.txt")
                    'Adding each file to the ListBox and the FileList at the same time keeps the indexes of
                    'the files in the ListBox and FileList the same. That will let us get the Full path and name
                    'of the file from the list according to the ListBox`s selected index

                    FileList.Add(f) 'Add each text file`s full path and name to the FileList
                    ListBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(f)) 'add just the filename to the listbox
                Next

            End If
        End Using
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'Read all text from the file in the FileList that is at the index of the ListBox`s Selected Index
        'and assign it to TextBox2
        If ListBox1.SelectedIndex > -1 Then
            TextBox2.Text = IO.File.ReadAllText(FileList(ListBox1.SelectedIndex))
        End If
    End Sub
End Class



Hope it helps. B)
Was This Post Helpful? 0
  • +
  • -

#10 HappyMan20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-July 14

Re: Trying to extract listbox text files in to textbox

Posted 15 July 2014 - 07:48 PM

Nothing is working. I have figured out how to make the listbox show the directory or just the file name. Im trying to have the textbox read the text file that is selected in the listbox. I have been trying different things. I used this(below) and different variations. However using the one below I get an error with a line under the 1st objreader it says

"overload resolution failed because no accessible 'new' can be called without a narrowing conversion: 'public sub new﴾path as string﴿': argument matching parameter 'path' narrows from 'object' to 'string'. 'public sub new﴾stream as system.io.stream﴿': argument matching parameter 'stream' narrows from 'object' to 'system.io.stream'."

Dim objreader As New System.IO.StreamReader(ListBox1.SelectedItem)
TextBox2.Text = objreader.ReadToEnd
objreader.Close()

Was This Post Helpful? 0
  • +
  • -

#11 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 221
  • View blog
  • Posts: 735
  • Joined: 01-February 13

Re: Trying to extract listbox text files in to textbox

Posted 15 July 2014 - 08:01 PM

Hi,
Make a New Form project and add 2 TextBoxes, 1 ListBox, and 1 Button to the form. Set TextBox2 to Multiline = True but, don`t rename anything. Then copy the code i just posted exactly as it is. It works fine for me. Using a StreamReader is just complicating your code when you could just use the System.IO.File.ReadAllText method.

If you have problems with it then i will try to help in the morning before i go to work. B)
Was This Post Helpful? 0
  • +
  • -

#12 HappyMan20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-July 14

Re: Trying to extract listbox text files in to textbox

Posted 15 July 2014 - 11:20 PM

View PostIronRazer, on 15 July 2014 - 08:01 PM, said:

Hi,
Make a New Form project and add 2 TextBoxes, 1 ListBox, and 1 Button to the form. Set TextBox2 to Multiline = True but, don`t rename anything. Then copy the code i just posted exactly as it is. It works fine for me. Using a StreamReader is just complicating your code when you could just use the System.IO.File.ReadAllText method.

If you have problems with it then i will try to help in the morning before i go to work. B)/>


Ok I just copied it all over and it worked. The textbox read the listbox items finally thanks for helping on that part. Im gonna play with it more tomorrow to have it search the text files.
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 221
  • View blog
  • Posts: 735
  • Joined: 01-February 13

Re: Trying to extract listbox text files in to textbox

Posted 16 July 2014 - 02:53 AM

Hi,
I guess i missed the part where you wanted to only add the files that have the @ character in them. You can use the same IO.File.ReadAllText method to read the files into a temporary string and then use the String.Contains method to check if they contain a @. If they do then add them to the FileList and to the ListBox.

You can change a few lines in the Button click event from my prior example so that it does that like this
Public Class Form1
    Private FileList As New List(Of String) 'The list that will hold the full path and name to each file

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using fb As New FolderBrowserDialog
            If fb.ShowDialog = DialogResult.OK Then

                ListBox1.Items.Clear() 'Clear all items from the ListBox
                FileList.Clear() 'Clear all file pathnames from the FileList

                TextBox1.Text = fb.SelectedPath

                For Each f As String In IO.Directory.GetFiles(fb.SelectedPath, "*.txt")

                    Dim tmp As String = IO.File.ReadAllText(f) 'read the current file into a string

                    'if the (tmp) string contains the @ character then add it to the ListBox and FileList
                    If tmp.Contains("@") Then
                        FileList.Add(f) 'Add each text file`s full path and name to the FileList
                        ListBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(f)) 'add just the filename to the listbox
                    End If
                Next

            End If
        End Using
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'Read all text from the file in the FileList that is at the index of the ListBox`s Selected Index
        'and assign it to TextBox2
        If ListBox1.SelectedIndex > -1 Then
            TextBox2.Text = IO.File.ReadAllText(FileList(ListBox1.SelectedIndex))
        End If
    End Sub
End Class



Hope it helps. B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1