Results 1 to 7 of 7

Thread: load txt file into a textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Posts
    294

    load txt file into a textbox

    does anyone know a quick way to load a txt file into a textbox? the .txt file has a pretty long string in it, so that's why i would like to just load it into a textbox to make it easier on me. I cant find the code to load a txt file into a textbox for some reason, even though i thought it would be easy.

  2. #2
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: load txt file into a textbox

    VB Code:
    1. Public Sub DocToTxt(Path As String, TxtBox As TextBox)
    2.   Dim ff As Integer
    3.   Dim StrLine As String
    4.  
    5.   Open Path For Input As ff
    6.  
    7.   Do Until EOF(ff)
    8.      Line Input #ff, StrLine
    9.      TxtBox.Text = TxtBox.Text & StrLine & vbNewLine
    10.   Loop
    11.  
    12.   Close ff
    13. End Sub

    Try that.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: load txt file into a textbox

    That should work, except that it is about the slowest possible way to do it.
    Quote Originally Posted by Whatupdoc
    does anyone know a quick way to load a txt file into a textbox?
    VB Code:
    1. Public Sub DocToTxt(ByRef pszFilename As String, ByRef pTextbox As Textbox)
    2. Dim hFile As Long
    3.     hFile = FreeFile()
    4.     Open pszFilename For Binary Access Read Lock Write As #hFile
    5.     pTextbox.Text = Input(hFile, LOF(hFile))
    6.     Close #hFile
    7. End Sub

  4. #4
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: load txt file into a textbox

    Quote Originally Posted by penagate
    That should work, except that it is about the slowest possible way to do it.
    VB Code:
    1. Public Sub DocToTxt(ByRef pszFilename As String, ByRef pTextbox As Textbox)
    2. Dim hFile As Long
    3.     hFile = FreeFile()
    4.     Open pszFilename For Binary Access Read Lock Write As #hFile
    5.     pTextbox.Text = Input(hFile, LOF(hFile))
    6.     Close #hFile
    7. End Sub
    Sorry, it's the only way I knew how to do it Yes that would be faster, although I would of never thought of that.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: load txt file into a textbox

    Well now you know!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Posts
    294

    Re: load txt file into a textbox

    it doesnt seem to work. i have that function in a module then i called it from form_load using this code: Call DocToTxt(App.Path & "\data.txt", Text3)

    there is a file called data.txt in the same directory as the program and there's also a textbox called text3.text. I get an error "Bad file name or number" then it points to pTextbox.Text = Input(hFile, LOF(hFile))

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: load txt file into a textbox

    Sorry I had the parameters round the wrong way

    It should be
    VB Code:
    1. pTextbox.Text = Input$(LOF(hFile), hFile)

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