Results 1 to 11 of 11

Thread: How to put file contents to a text box?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Unhappy

    Hi VB Guys and VB Gals,

    Need some help here with a VB code, hope you can help me out or give some ideas, anything!

    How do I get my file contents from a .txt file to appear on my textbox?

    Here's my code....

    Private Sub CmdView_Click()
    Dim myline As String
    Open "d:\mydocuments\input.txt" For Input As #1
    Do Until EOF(1)
    Line Input #1, myline
    TextBox1.Text = myline
    Loop
    Close #1
    Close #2
    End Sub

    Basically, it reads the contents of input.txt by line and writes it to the textbox.... but it keeps on overwriting the first line. Is there some kind of WRITELN function in VB? What do you do to completely write down the whole contents of the file to the textbox?

    Thanks! =)
    Thanks for reading my post!

  2. #2
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    Your code says the textbox text should equal the new input. It should say the textbox text equals the old textbox text & the new input.


    so TextBox1.text = TextBox1.text & myline

    also make sure your textbox is multiline = true

    [Edited by billwagnon on 05-24-2000 at 02:58 PM]

  3. #3
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Another solution

    Instead of looping through your text file line by line, you can load the txt file into the text box in one fell swoop using the Input$ function and the LOF (length-of-file) function, as follows:

    Open "C:\MyFile.txt" For Binary As #1
    Text1.Text = Input$(LOF(1), #1)
    Close #1

    By the way, your textbox should have its multi-line property set to True, and scrollbars property set to Vertical.

    If you don't want the user to modify the text, set the Locked property to True (use Locked rather than Enabled - if you set Enabled to False, the user will not be able to scroll the text). Bottom line: both Locked and Enabled should be True.

    Also, opening for Binary is more reliable than opening for Input in this situation.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Talking Cool Stuff!

    Hi Billwagnon and BruceG,

    Thanks for your reply! This is such cool stuff. I think everyday that I learn more VB just becomes more and more addictive and exciting!

    Follow up question to BruceG, I used your method and it worked like a charm...! Is there a way to count the number of lines in my text file, or do I have to do a loop and count it one by one by using a counter? If I do this then I have to go back to Open file as input? Or is there a way to do this even if I use Open file as binary?

    Thanks again guys!
    Thanks for reading my post!

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I don't see the good point having textbox containing the file content. Textboxes can't contain any nullchars (chr(0)) so you will probably have it corrupted if you put something that's not formatted as plain text. Here's the fast way to get your file as a string property:

    Code:
    Property Get File(Filename As String) As String
        Dim fnum As Byte
        fnum = FreeFile
        Open Filename For Binary As fnum
            File = Space(LOF(fnum))
            Get #fnum, , File
        Close fnum
    End Property
    Property Let File(Filename As String, textstring As String)
        Dim fnum As Byte
        If Dir(Filename) <> "" Then Kill Filename
        fnum = FreeFile
        Open Filename For Binary As fnum
            Put #fnum, , textstring
        Close fnum
    End Property
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Response to follow-up

    To count the lines, you'll have to go back to the loop method, with billwagon's correction (concatenating the lines as you go). I would only open as Binary when using my method (Input$ and LOF functions). When reading the file "normally", you should open as Input. (The reason I use Binary for the other method is that sometimes VB will generate an "Input past end of file" message when the file is opened as Input. Also, when using Binary, you can load non-text files into the textbox - although non-ASCII characters in those files will display as "hieroglyphics".)

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Bruce, that's not it. You wont get any "Input past end of file" if you have eof checking for it:
    Code:
    If not EOF(0) then Input...
    The thing is that input is slow because it checks for "," chr(13)+chr(10) and chr(13) while getting the file. The textbox is still not good alternative for placing the text, if it contains binary stuff, the nullstring will cut off the rest of file.

    Hi zoee, you could still open in binary and split up the string into an array with split function (I haven't used it as i use vb5 and it's a vb6 function) and having the vbcrlf as keyword.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Junior Member
    Join Date
    May 2000
    Location
    Milwaukee, Wi
    Posts
    28
    The Text Box in VB is defaulted to Multiline false. what this means is that no matter how large you make the text box, it will only write one line, and then overwrite as needed. If you want to change it, during design time, click on the text box and look at its properties. Find the "Multiline" property and change it to true. You can also do this during run-time with: text1.multiline = true

    now you are set

    Shaheeb R.

  9. #9
    Addicted Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    145
    If you use a RichTextBox instead of a plain TextBox you can use the code:

    Text1.Loadfile filename, rtf

    ' where filename is a string, containing tha full path and
    ' name, e g "C:\windows\mytext.txt"

    It is fast, simple and it works for big files (up to two Gb, I think).

    Pentax
    Wilhelm Tunemyr,
    Swede in London

    [email protected]

    "Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
    Heinrich Heine (1797-1856)

    Pravda vítezi!
    (Truth prevails!)

  10. #10
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    Re: How to put file contents to a text box?

    Quote Originally Posted by Pentax
    If you use a RichTextBox instead of a plain TextBox you can use the code:

    Text1.Loadfile filename, rtf

    ' where filename is a string, containing tha full path and
    ' name, e g "C:\windows\mytext.txt"

    It is fast, simple and it works for big files (up to two Gb, I think).

    Pentax

    Brilliant That works a treat, I was wondering how to get my file into a text box. An ordinary text box woouldn't have it. Thanks.

  11. #11
    Addicted Member acidsnake's Avatar
    Join Date
    Feb 2008
    Posts
    157

    Re: How to put file contents to a text box?

    you can use label to handle more content.

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