Results 1 to 11 of 11
  1. #1
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Display lots of text in VB (VB6)

    I'm writing some code and at some time want to present the user with a box in which I supply some text. Typically, the text will vary on the previous responses and is quite large.. maybe one page (I guess I'd like scroll bars too). How would I do that in VB6? What controls to use etc.? (the text should be read-only).

    I'd also like the text to be printed out, better (and acceptable) would be is the user could select all and copy to the clipboard (maybe by pressing a button???)

    Looking forward to any help you might give, ...pieces of code would be even nicer ;-)

    Erik Jan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Display lots of text in VB (VB6)

    Erik Jan,

    If you want to display plain text, you can use a standard Text Box control. Set its Locked property to True, so that the user can't edit the text; if you leave the Enabled property True (the default setting), the user can select and copy text. Set the Scrollbars property to 2 - Vertical.
    If you want to copy the text to the clipboard, use Clipboard.SetText method: Clipboard.SetText Text1.Text, where Text1 is the name of the text box control.

    If you want to display formatted text, use the Rich Textbox control. If you don't have it on your Toolbox, right-click the Toolbox, select Components..., then check Microsoft Rich Textbox Control 6.0 and click OK. This control has a SelPrint method - see the online help or MSDN.

  3. #3
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Display lots of text in VB (VB6)

    As Hans, said, the RTF control is the way to go. It also supports scroll bars and <ctrl> X, C & V do Cut, Copy & Paste with no stinkin' coding. HTH --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  4. #4
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display lots of text in VB (VB6)

    Hans & Sam,

    Thanks for the tips, I'll certainly try this out.... There's one loose end however (which was -I guess- a bit hidden in my original request): as I want the text to vary, I'll need to fill the text/RTF-box with the appropriate text during run-time. In addition, I'd like all possible texts to be incorporated into the code (don't like to have separate text-files).

    How would I go about doing that (maybe data-statements? Do these still exist?)... will texts auto-wrap if the user sizes the text-box? How do I add formatting in the texts (if I'd use RTF-boxes); e.g. hard-CR/LF's, bold-face, color???

    Looking forward to your responses!

    Thanks again for helping me out here!

    Erik Jan

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Display lots of text in VB (VB6)

    One way to fill the Rich Textbox is to set the TextRTF property. You'd have to generate RTF for that. You could either learn the RTF specification <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15> or save a formatted text as .rtf file and use that as a starting point. I'd suggest using WordPad, not Word for this, because .rtf documents created with Word contain much more overhead. If you open a simple .rtf document in NotePad, you will see how it is structured.

    Another way is to set the plain text of the Rich Text box, select parts of it and manipulate the properties of the selected text. Here is an example:

    Private Sub Form_Initialize()
    With RichTextBox1
    ' Set Text
    .Text = "Hello World, how are you today?" & vbCrLf & "This is written in Visual Basic."
    ' Select "World" and make it bold (offsets are zero-based)
    .SelStart = InStr(.Text, "World") - 1
    .SelLength = Len("World")
    .SelBold = True
    ' Select "Basic" and make it italic
    .SelStart = InStr(.Text, "Basic") - 1
    .SelLength = Len("Basic")
    .SelItalic = True
    ' Unselect text
    .SelStart = 0
    .SelLength = 0
    End With
    End Sub

    And yes, the text will wrap automatically when the text box is resized.
    Note that the MultiLine property of the text box must be set to True (the default).

  6. #6
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display lots of text in VB (VB6)

    OK, that wraps up the formatting etc.

    Problem left would be: how to get the texts into the code?

    I now have several pages in WORD -representing maybe 30 different texts I want to display depending of the outcome of my program (I can export these into RTF and/or export as plain text and do the formatting as you indicated - which might be good enough as I probably want to bold/color titles only)

    Erik Jan

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Display lots of text in VB (VB6)

    The easiest way would be to store the texts as .rtf files, and load these into the Rich Textbox control using its LoadFile method, but you have indicated that you don't want to work with separate files.

    Otherwise, you'll have to break your texts into paragraphs and concatenate these in code, like in my simple example:

    Dim strText As Text
    strText = "Text for paragraph 1." & vbCrLf & _
    "Text for paragraph 2." & vbCrLf & _
    "Text for paragraph 3."
    RichTextbox1.Text = strText

    This becomes very tedious, of course, but it allows you to store bits of text in variables and re-use these.

    Another method is to store the texts in a resource file that will be compiled into your .exe. Load the Resource Editor add-in, if you don't have it loaded already; this will add items to the Project and Tools menu. Once you have created a resource file with a string table, you can retrieve texts using LoadResString. See the online help or MSDN for details.

  8. #8
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Display lots of text in VB (VB6)

    I like Hans's suggestion of a resource file. Armed with his WordPad hint and the TextRTF property, RTF dosn't look too bad either. I created a document in WordPad, then loaded it into Notepad, and copied it into VB:
    <pre>Option Explicit

    Private Sub Form_Load()
    RichTextBox1. _
    TextRTF = "{rtf1ansiansicpg1252deff0deflang1033" _
    & "{fonttbl{f0fswissfcharset0 Arial;}}" & vbCr _
    & "{colortbl ;red0green0blue255;}" & vbCr _
    & "viewkind4uc1pardf0fs20" _
    & "This is b bold b0 and cf1 bluecf0 .par" & vbCr _
    & "}"
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  9. #9
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display lots of text in VB (VB6)

    OK, I've read a bit about the Resource Editor add-in and feel that might be an elegant way to go (will tackle the formatting [RTF-codes] after that). Played around a bit and see now how this could work fairly easy. Have one problem though: how do I get all my existing info into the RES file??? I noticed I can copy & paste manually only line-by line... that would a quite a task as I envision hundreds of lines (20% of which I've already programmed in code and should be transferred, 80% is in WORD files).

    Looking at the example-code in the documentation of my VB6 installation I found that two files seem to describe resources: a file with extension RC (which seems to be TEXT) and one with extension RES. Playing around with VB6 I could only do something with the RES-file.. regretfully only the add-in seems to allow access to that one... (is the RC-file a remainder of VB5 were I seem to have understood this was done via an external editor??)

    Question: can I build a RES file myself simply by dropping-in loads of text (after having tweaked that into the right format using one of my editors), starting with an RC-file would be OK, the format seems rather simple... Any tools (freeware if possible... this might be a one time effort) out there [time-lock would be OK if no freeware]???

    Erik Jan

  10. #10
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Display lots of text in VB (VB6)

    The RC file is a sort-of readable text file. There is a compiler on the Visual Studio CD to convert RC file to RES files. But, looks like the best way is the Excel to RES file converter from Planet Source Code. I haven't used either of these, so I really don't know which would be the better way. Post back and let us know if it was easy to use. --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  11. #11
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display lots of text in VB (VB6)

    Loaded the code you mentioned, looks great! I'll give this a try and will let you know.

    Erik Jan

    PS. On one system (Win98) I can compile/run the code, on an other system (WinXP) I get an error with a missing OCX (DBGRID32 or something). If I then compile on the 'good' system and install the EXE on the other system the EXE does run but generates error "Couldn't find installable ISAM"....
    No major problems, I'll just have to do all on the first system... just wondering however why this is (thought that if one installed an app, everything required would come with it...)

Posting Permissions

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