Results 1 to 4 of 4

Thread: [VB6] BigTextBox - Log Large Amounts of Text to a TextBox

  1. #1

    Thread Starter
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Lightbulb [VB6] BigTextBox - Log Large Amounts of Text to a TextBox

    Nothing seems to spook the herd here like using external libraries.

    "Sweet sacred boa of Western and Eastern Samoa!" they gasp. "A dependency!"

    The reason for this fear escapes me, since Registration Free COM has eliminated most of the bottlenecks for almost a decade. Still, suggesting the use of a RichTextBox for logging and displaying text beyond 32K characters does add another large library to distribute.


    Why not just use the intrinsic TextBox control? Well some manipulations via its character position and size properties can cause grief over 65,535 characters. And when the text gets long, the concatenation is a killer so you need those properties.

    But by sending a few strategic messages to a TextBox control's underlying control window we can get around many of these limitations. The attached demo Project shows how to go about it.
    Attached Files Attached Files

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

    Re: [VB6] BigTextBox - Log Large Amounts of Text to a TextBox

    Is it OK to use it like this? (see note at bottom)

    Code:
    Private Sub OpenFile(ByVal sPath As String, TxtBox As TextBox)
        ' // Load Text File //
        Dim ff As Integer
        ff = FreeFile
        On Error GoTo Fini
        ' clear old text
        TxtBox.Text = vbNullString
        ' set margins
        SendMessageWLng TxtBox.hwnd, EM_SETMARGINS, EC_LEFTMARGIN Or EC_RIGHTMARGIN, LoHi(2, 2)
        ' open file
        Open sPath For Input As #ff
        ' load contents (Use .SelText) <<<<
        TxtBox.SelText = Input(LOF(ff), ff)
        ' set vert scroll to top
        SendMessageWLng TxtBox.hwnd, EM_SCROLL, SB_TOP, 0&
        Close #ff
        Exit Sub
    Fini:
        Close #ff
        MsgBox Err.Description, vbInformation
    End Sub
    On one text file (987K) at the very start of the text I see 3 odd characters that aren't in the file. If I save the text box to a new file the chars aren't there in notepad but show up in BigTextBox, something wrong with my open file sub?
    Attached Images Attached Images  

  3. #3

    Thread Starter
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [VB6] BigTextBox - Log Large Amounts of Text to a TextBox

    Ahh, they are in the file but you won't see them using Notepad. Notepad is Unicode-aware and handles things like a UTF-8 BOM. Those first 3 characters would be the BOM. A BOM is metadata, not characters.

    It looks very much as if you are trying to treat a UTF-8 file as an ANSI (not ASCII) file.

    Classic VB (4, 5, 6) native I/O statements treat text files as ANSI on disk encoded using the current system codepage, and translate this data to UTF-16LE (what Windows calls "Unicode") on read and from Unicode on write.


    Windows systems really should never have UTF-8 files on them. However people have "bent over" for the open source crowd who mainly tout Unix/Linux standards. It doesn't help that so many kowtow to the PHP mentaility either - which has a Unix/Linux dominated philosophy. Those systems have very poor Unicode support natively, and often fall back on UTF-8 because much of the time ASCII text can be claimed to be UTF-8.

    ANSI characters sets on Windows look a lot like ASCII with the addition of character codes from 128 to 255. UTF-8 looks the same as ASCII as long as you never use characters above 127.


    To properly read a UTF-8 file in VB6 you have three basic choices:
    • Use the ADO.Stream object, which can be used to read and write files of many encodings including most Unicode variations such as UTF-8.
    • Use VB6 binary I/O and do a proper manual translation of the encoded text.
    • Read it as text, strip off the BOM bytes (whch will look like 3 strange characters in a VB String), and hope for the best (no characters above 127).

    Note that in UTF-8 encoding characters between 0 and 127 will be represented by one byte. Characters in other ranges each take up two or more bytes in the file. UTF-8 is a variable character length encoding.

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

    Re: [VB6] BigTextBox - Log Large Amounts of Text to a TextBox

    Quote Originally Posted by dilettante View Post
    Ahh, they are in the file but you won't see them using Notepad.
    OK, The file was originally created by firefox (bookmarks export to html) i just copied it to make it larger and renamed the ext to txt. All other (regular) text files i tried look ok.

    Thanks for the explanation!

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