CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 1999
    Posts
    22

    Richtextbox-displaying text from a database

    I want to view reports in visual basic. when I read data out of a database and send it to a richtextbox
    it overwrites the first line of the richtextbox over and over again. This results in the last line of the
    report showing in the richtextbox only. How do I make the richtextbox show each line of the report.
    I have used a listbox with a list1.additem statement, but I would prefer to use a richtextbox so I can
    controll fonts and size of text as it is displaying in the richtextbox.


  2. #2
    Join Date
    Oct 1999
    Location
    S.W. Wyoming
    Posts
    25

    Re: Richtextbox-displaying text from a database

    This is cumbersome; even clumsy, but CAN be managed. Perhaps if somebody more clever than I responds, you might get a more elegant solution. In my instance, I am adding lines for an event list and want the lines in colors, fonts, etc., much as you have indicated. This is how I solved the problem:


    ' Declare variables

    Dim zx0$ ' a "throwaway" string variable

    ' Begin loop of accumulating text for the events [your report data would be
    ' assembled here in this example].

    If gEvents.RecordCount > 0 then
    gEvents.MoveFirst

    ' Stuff the string where we will accumulate the
    ' text to be displayed with the "prelude" required for
    ' a rich text box. This contains the font and color tables.

    zx0$ = "{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\fswiss MS Sans Serif;}{\f3\froman Times new Roman;}}{\colortbl\red0\green0\blue0;red0\green0\blue255;\red0\green255\blue0;\red255\green0\blue0;\red255\green0\blue255;}\deflang1033\pard\plain\f2\fs21 "

    ' Now begin accumulating text. Replace any "\par }"
    ' strings (which mark the end of ALL text) with
    ' only the paragraph code \par.


    Do
    If DateDiff("n", gEvents("date"), Now) <= 10 then
    zx0$ = zx0$ & "\cf3 [" & Format$(gEvents("date"), "dd-mmm-yy hh:mm") & "]" & Chr$(9) & "\cf2 " & GetR(gEvents, "Event") & "\par " ' Text is in one color if date is within last 10 days
    else
    zx0$ = zx0$ & "\cf4 [" & Format$(gEvents("date"), "dd-mmm-yy hh:mm") & "]" & Chr$(9) & "\cf1 " & GetR(gEvents, "Event") & "\par " ' Text is in another color if date is older than 10 days
    End If

    ' get the next event record. Or, the next line of your report.

    gEvents.MoveNext
    Loop While Not gEvents.EOF

    ' Move the text into the rich text box, adding the
    ' "end of all text" closing space/brace

    RichTextBox1.TextRTF = zx0$ & " }"

    ' "Scroll" to the bottom

    RichTextBox1.SelStart = len(RichTextBox1.Text)

    ' Show the reminder box now

    Show vbModeless
    End If





    The contents of the "prelude" are simply what I get with the TextRTF property of a RichTextBox when it contains nothing. You'll note that it defines the font tables and color tables. The actual lines are coded for font and color by referring to these two "prelude" tables (\cf1, for example, references color 1 in the color table (and I seem to recall the tables are zero-based); fonts are handled the same way but with a different tag. One way of identifying all the tags you'll want for your report is to fill a Rich Text box with a sample of what you want the report to look like (create it with your WP and paste it in) and examining the contents of the TextRTF property. You'll find \ul (underline}, "\b" (bold}, "\i" (italic}, etc. Study what tags are used for each font or font attribute you use, get the tag (which indexes into the font table) and use them like the "\cf1" tags in my sample above. Of course, your font table and color table will be different, based on your sample report.

    I know this is inelegant: the problem was not easy of solution. And, as I say, maybe some really brilliant person can provide you a better solution. This has the virtue of working, at least. Good Luck!


    Reid Allen Robbins
    Green River, WY 82935

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

Featured