Results 1 to 7 of 7

Thread: Simple questions

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    How do I load a normal text file into a TextBox, like a readme file for instance?

    And how would I go about counting the number of characters?

    Thanks.

    -Git

  2. #2
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Code:
    'Module
    'Not my code  (It's not good enough hehe ;) )
    Function ReadTextFileContents(filename As String) As String
        Dim fnum As Integer, isOpen As Boolean
        On Error GoTo Error_Handler
        ' get the next free file number
        fnum = FreeFile()
        Open filename For Input As #fnum
        ' if execution flow got here, the file has been open without error
        isOpen = True
        ' read the entire contents in one single operation
        ReadTextFileContents = Input(LOF(fnum), fnum)
        ' intentionally flow into the error handler to close the file
    Error_Handler:
        ' raise the error (if any), but first close the file
        If isOpen Then Close #fnum
        If Err Then Err.Raise Err.number, , Err.Description
    End Function
    
    'Form
    Private Sub Command1_Click()
    Text1.Text = ReadTextFileContents("C:/readme.txt")
    End Sub
    And number of characters:

    Code:
    Private Sub Command2_Click()
    MsgBox("Length in characters: " & Len(Text1.Text), 64, "Info")
    End Sub
    Hop this is what you wanted,

    Me
    Courgettes.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Thanks. =)

    There's an error though :

    C2.Caption = Len(Text1.Text), 64, "Info"

    All it says is 'Expected: End Of Statement' on the first comma.

    Any idea what's wrong?

    -Git

  4. #4
    Guest
    Code to open file:
    Code:
    Open "C:\Windows\Desktop\ttt.txt" For Input As #1
    Text1 = Input(LOF(1), 1)
    Close #1
    Regarding your number of character's problem:
    Code:
    MsgBox ("File is " & Len(Text1) & " chars long")

  5. #5
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    Originally posted by git
    Thanks. =)

    There's an error though :

    C2.Caption = Len(Text1.Text), 64, "Info"

    All it says is 'Expected: End Of Statement' on the first comma.

    Any idea what's wrong?

    -Git
    The code
    Code:
    Len(Text1.Text), 64, "Info"
    is for use only in a MsgBox - if you are only going to
    caption it, use
    Code:
    C2.Caption = Str(Len(Text1.Text))
    you don't need the .Text, either. It is the default property
    of a text control.
    Donald Sy - VB (ab)user

  6. #6
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Yeah, 64 is just a quick way to set the parameters for the MsgBox and "Info" is the MsgBox's title. Sorry if that mislead you.

    Courgettes.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Ok, thanks all! =)

    -Git

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