Results 1 to 18 of 18

Thread: [RESOLVED] Need help with terminology and file management

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Resolved [RESOLVED] Need help with terminology and file management

    Hey all,

    I have been attempting to learn and use VB2013 and am making fair progress. I try to look things up before I post but sometimes I am not sure what to look for.

    I have this code.

    Code:
    Public Class Form1
    
        Private Sub ReadTheFile_Click(sender As Object, e As EventArgs) Handles ReadTheFile.Click
    
            Dim OriginalTextFile As String = "C:\Users\Bryant\Google Drive\readthisfile.txt"
            Dim FileText As String
            Dim OTFReader As New System.IO.StreamReader(OriginalTextFile)
            FileText = OTFReader.ReadToEnd
            TextBox.Text = FileText
    
        End Sub
    End Class
    It just reads a hard coded text file when a user clicks a button and writes whatever is in the file to the textbox.

    What would the code look like that would allow a user to select their own text file and then write the contents of whatever file the user selected into the text box when the button is clicked.
    In case I am not clear this will be two steps. One the user selects the file. Two the user clicks the button to write the contents into the box.

    If there is a tutorial on this somewhere that may already exist I am fine with being directed to it.

    Thanks.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Need help with terminology and file management

    Quote Originally Posted by bwquestion View Post
    What would the code look like that would allow a user to select their own text file and then write the contents of whatever file the user selected into the text box when the button is clicked.
    I normally use, OpeFileDialog , and check the return value, DialogResult and if OK then load text file with File.ReadAllText


    Code:
    Private Sub ReadTheFile_Click(sender As Object, e As EventArgs) Handles ReadTheFile.Click
    
      Using ofd As New OpenFileDialog
          ofd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
          If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
              TextBox1.Text = IO.File.ReadAllText(ofd.FileName)
              'Else
              'MessageBox.Show("No file was selected by user")
          End If
      End Using
    
    End Sub
    Last edited by Edgemeal; Aug 20th, 2014 at 04:35 PM. Reason: Fix links

  3. #3
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Need help with terminology and file management

    The OpenFileDialog class does what you are asking. Also condensed the code a bit:

    Code:
            Using ofd As New OpenFileDialog
                ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
                If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                    TextBox1.Text = My.Computer.FileSystem.ReadAllText(ofd.FileName)
                End If
            End Using
    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    These do not quite seem to do what I am trying to do. The end result should have two buttons. The User would click the first button to select the file to be read. Based on the above recommendations I believe it woudl look something like this:

    Code:
        Private Sub ReadTheFile_Click(sender As Object, e As EventArgs) Handles SelectAFile.Click
    
            Using ofd As New OpenFileDialog
                ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
                If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim FileName As String
                    FileName = IO.File.ReadAllText(ofd.FileName)
                End If
            End Using
        End Sub
    Now that the user has selected the file I would like to have a second button to use the file selected and read its contents into a string value that can be used for other purposes. Look at how the example is written. I would like a user to be able to select a file of choice and not have to hard code the path. The rest should work the same. Both of the examples seem to be reading the contents directly into a text box. I do not understand enough about VB to know how to use or manipulate the text if it is in a text box. Thanks

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    This is an idea of what I am trying to do.

    Code:
        Dim FileName As String
    
        Private Sub ReadTheFile_Click(sender As Object, e As EventArgs) Handles SelectAFile.Click
    
            Using ofd As New OpenFileDialog
                ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
                If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                    FileName = IO.File.ReadAllText(ofd.FileName)
                End If
            End Using
        End Sub
    
    
        Private Sub ReadTheFile_Click_1(sender As Object, e As EventArgs) Handles ReadTheFile.Click
    
            Dim FileText As String
            Dim OTFReader As New System.IO.StreamReader(Filename)
            FileText = OTFReader.ReadToEnd
            TextBox.Text = FileText
    
        End Sub
    When I clcik the SelectAFile button it seems to work. When I click the ReadThe File button it gives me an error that there are Illegal characters in the path.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Need help with terminology and file management

    that's an overcomplicated method you're using, but:

    Code:
    FileName = IO.File.ReadAllText(ofd.FileName)
    should be:

    Code:
    FileName = ofd.FileName

  7. #7
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Need help with terminology and file management

    Quote Originally Posted by bwquestion View Post
    These do not quite seem to do what I am trying to do. The end result should have two buttons. The User would click the first button to select the file to be read. Based on the above recommendations I believe it woudl look something like this:

    Code:
        Private Sub ReadTheFile_Click(sender As Object, e As EventArgs) Handles SelectAFile.Click
    
            Using ofd As New OpenFileDialog
                ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
                If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim FileName As String
                    FileName = IO.File.ReadAllText(ofd.FileName)
                End If
            End Using
        End Sub
    Now that the user has selected the file I would like to have a second button to use the file selected and read its contents into a string value that can be used for other purposes. Look at how the example is written. I would like a user to be able to select a file of choice and not have to hard code the path. The rest should work the same. Both of the examples seem to be reading the contents directly into a text box. I do not understand enough about VB to know how to use or manipulate the text if it is in a text box. Thanks
    You are almost there:

    Code:
    Dim FileName As String
    
        Private Sub ReadTheFile_Click(sender As Object, e As EventArgs) Handles SelectAFile.Click
    
            Using ofd As New OpenFileDialog
                ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
                If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                    FileName = ofd.FileName
                End If
            End Using
        End Sub
    
    
        Private Sub ReadTheFile_Click_1(sender As Object, e As EventArgs) Handles ReadTheFile.Click
    
            TextBox.Text = IO.File.ReadAllText(FileName)
    
        End Sub
    Optionally, you do not have to use StreamReader . StreamReader is more complex than your needs.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    Thanks again .paul. your correction worked. Tell me about the overcomplicated method. I do not know what that means. It also will not let me rate your post.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    jayinth813. Where in your example does the code store the information that is read in from the selected file into a string for use at a later time? That is the part I could not figure out.

    I use streamreader because I know it will get everything in the file. Things like linereturns and other things that are not necessarily text. When I see something that says ReadAllText it makes me think it would not pick up everything in the file. It may very well be the same thing I just do not know enough about it to use it at this point.
    Last edited by bwquestion; Aug 21st, 2014 at 10:10 PM.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Need help with terminology and file management

    Quote Originally Posted by bwquestion View Post
    Thanks again .paul. your correction worked. Tell me about the overcomplicated method. I do not know what that means. It also will not let me rate your post.
    As jayinth813 told you:

    Code:
    TextBox.Text = IO.File.ReadAllText(FileName)
    Quote Originally Posted by bwquestion View Post
    jayinth813. Where in your example does the code store the information that is read in from the selected file into a string for use at a later time? That is the part I could not figure out.
    It doesn't save the file contents.
    The first button just gets the filename which it saves in the FileName variable
    The second button reads the file using the saved filename.
    No need to use a StreamReader

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Need help with terminology and file management

    readalltext returns everything in the file...

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    I may just be missing something really obvious so please humor me. If I read the contents of the file directly to a textbox, how would I be able to do anything with it. Say we need to replace all the occurrences of something in the file with something else. If it is stored in a string we could do this very easy. If we skip that step we would have to read the file in again to do anything with it. What part am I missing?

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Need help with terminology and file management

    There are 256 possible characters in use on English and us computers, and they all have a textual representation, though depending on which font you use they may appear as spaces.
    Windows recognizes all of those characters...

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Need help with terminology and file management

    It is stored in your textbox's .Text property

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    I changed the streamreader to ReadAllText and it does fine. Here is the complete code.

    Code:
        Dim FileName As String
    
        Private Sub ReadTheFile_Click(sender As Object, e As EventArgs) Handles SelectAFile.Click
    
            Using ofd As New OpenFileDialog
                ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
                If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                    FileName = ofd.FileName
                End If
            End Using
        End Sub
    
    
        Private Sub ReadTheFile_Click_1(sender As Object, e As EventArgs) Handles ReadTheFile.Click
    
            Dim FileText As String
            FileText = IO.File.ReadAllText(FileName)
            TextBox.Text = FileText
    
        End Sub

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Need help with terminology and file management

    You know how to use a Form level variable. You used that for FileName. It's not a giant leap to see how you could store a file's contents in another variable...

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    I would not feel safe leaving data in a textbox.text property. The next time something was written to the textbox the original data would be gone. At least if it is in a string variable it is protected from things outside the sub. Maybe I'm just overcautious but it doesn't seem like too much extra to protect the data.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Need help with terminology and file management

    Thanks again .paul. This gives me a good understanding.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width