appending colored / fonted <?> text to a richtextbox

  • Thread starter Thread starter al jones
  • Start date Start date
A

al jones

Sorry, it's getting late and I'm tired.

I'm trying to fill a richtect box with text derived from the array I was
asking about earlier (thank you). I'm not sure what I'm seeing - since
most of the examples show how to load and save an rtf file into / from a
rtbox (may want to save this guy, so that'll come in handy, later).

Does anyone one know of a sample showing how to append, piecemeal, text
into an rtb with format changes as needed?? ((And if you do, would you
point me to it - I told you I'm getting tired :) ))

Going back to my example of MP3's, say I want the Artist in 14 pt, blue; on
a new line the album in 12 point, red and the songs of the album, each on a
seperate line in 12 pt black. Pointers, please //al
 
al jones said:
Sorry, it's getting late and I'm tired.

I'm trying to fill a richtect box with text derived from the array
I was asking about earlier (thank you). I'm not sure what I'm
seeing - since most of the examples show how to load and save an
rtf file into / from a rtbox (may want to save this guy, so
that'll come in handy, later).

Does anyone one know of a sample showing how to append, piecemeal,
text into an rtb with format changes as needed?? ((And if you do,
would you point me to it - I told you I'm getting tired :) ))

Going back to my example of MP3's, say I want the Artist in 14 pt,
blue; on a new line the album in 12 point, red and the songs of
the album, each on a seperate line in 12 pt black. Pointers,
please //al

W00t, a question I can (sort of) answer.

I've split the rtf into 5 sections, style header, colour header,
initialisation (other bits), main text, end. I don't think carriage
returns in the file make any difference to how it's presented, so you
might as well build up each line and writeline to the file.

For the style header, replace with whatever fits. Here's an example.
I'm not quite sure how each font is described, so you'll have to do
some experimenting yourself.
STYLE HEADER
{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1028{\fonttbl{\f0
\fmodern\fprq1\fcharset0 Courier New;}}

For the Colour header, replace with whatever fits. Note that colours
are referred to by the place in this section, so colour 1 is red255
\green0\blue0 and so on.
COLOUR HEADER
{\colortbl ;\red255\green0\blue0;\red0\green192\blue192;\red0\green255
\blue255;}

I don't know what all this means, but copying it en bloc seems to work.
INITIALISATION (OTHER BITS)
\viewkind4\uc1\pard\cbpat3\nowidctlpar\f0\fs20

For the main block of text, write whatever you want, but note that \par
means new line. Also note that \highlightn<space> changes background
colour to colour n (see colour header above), which will remain so
until you switch to another colour. Note that colour 0 will switch
highlighting off. Remember the space after n. If you want to change
the foreground colour, use cf instead of highlight.
MAIN TEXT
Rich \highlight1 text\highlight2 files suck\par
Badly and\highlight0 utterly

The end bit is just a curly bracket.
END
}

Copy the examples line by line into a textfile, rename it as *.rtf,
then play around with it to see how it works.

Does this means I'm an accepted member of the VB community now I've
answered a question?
 
al jones said:
I'm trying to fill a richtect box with text derived from the array I was
asking about earlier (thank you). I'm not sure what I'm seeing - since
most of the examples show how to load and save an rtf file into / from a
rtbox (may want to save this guy, so that'll come in handy, later).

Does anyone one know of a sample showing how to append, piecemeal, text
into an rtb with format changes as needed?? ((And if you do, would you
point me to it - I told you I'm getting tired :) ))

Mark the text using the control's 'Select' method and set the control's
'SelectionFont', 'SelectionColor', etc. properties accordingly.
 
Mark the text using the control's 'Select' method and set the control's
'SelectionFont', 'SelectionColor', etc. properties accordingly.

I've been to bed and still don't see that. I'm programatically creating a
line of text - how do I programatically 'select' the line which hasn't been
'printed' yet??. I'v been through the docs, seen the items you reference,
but don't see how to implement them. Thanks //al
 
Hi Al,

I haven't tried this, but I think one way to do it might be to :

1. First extract each piece of information (Artist, Album, Song...)
from your array.
2. To each piece, attach RTF codes as Yuk Tang suggested. You might
have to do a bit of research on this. BTW, if you find a good website,
that demystifies all those RTF codes, please let me know as well.
3. Make sure that no text is selected in the RTB, and then assign the
constructed RTF coded piece to the SelectedRtf property of your RTB.
This will append the text to the RTB with specified colors and fonts.

Do this in a loop till all your items have been covered.

Yuk Tang wrote :

Lolz ! Don't worry, you can exclusively *ask* questions all the time,
and still be an accepted member of the community. ;-)

Regards,

Cerebrus.
 
Cerebrus said:
2. To each piece, attach RTF codes as Yuk Tang suggested. You might
have to do a bit of research on this. BTW, if you find a good website,
that demystifies all those RTF codes, please let me know as well.

Rich Text Format (RTF) Specification, version 1.6
<URL:http://msdn.microsoft.com/library/en-us/dnrtfspec/html/rtfspec.asp>

For simple coloring I would stick with 'Select'/'SelectionColor' instead of
adding RTF code directly.
 
Rich Text Format (RTF) Specification, version 1.6 ... URL...

Thank you, Herfried. I've saved that in my "Favourites", and will study
it.

I suggested adding RTF coded text directly, since it might be
complicated to append a word to the RTB, then Select that word, and
then change it's font and color properties. Also, it might induce some
sort of flickering or the change might be noticeable to the user. I
haven't tried this, so I'm just guessing.

Or is there a way to set it's properties before the appended text is
displayed in the control ?

Thanks in advance,

Regards,

Cerebrus.
 
Cerebrus said:
I suggested adding RTF coded text directly, since it might be
complicated to append a word to the RTB, then Select that word, and
then change it's font and color properties. Also, it might induce some
sort of flickering or the change might be noticeable to the user. I
haven't tried this, so I'm just guessing.

You could either fill the control when it's not yet visible or prevent it
from redrawing:

Preventing controls from redrawing
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=disableredrawing&lang=en>
 
I've been to bed and still don't see that. I'm programatically creating a
line of text - how do I programatically 'select' the line which hasn't
been
'printed' yet??. I'v been through the docs, seen the items you reference,
but don't see how to implement them. Thanks //al

A crude example:-

Dim I As Integer, J As Integer, K As Integer
Dim S As String, T As String, U As String
With RichTextBox1
..Text = "This is some text "
..SelectAll()
..SelectionFont = New Font("Tahoma", 12, FontStyle.Bold)
..SelectionColor = Color.Chartreuse
I = .SelectionLength
S = "This is more text"
J = Len(S)
..Text &= S
..Select(I, J)
..SelectionFont = New Font("Arial", 8, FontStyle.Italic)
..SelectionColor = Color.Blue
..Select(0, 0)
End With
 
A crude example:-

Dim I As Integer, J As Integer, K As Integer
Dim S As String, T As String, U As String
With RichTextBox1
.Text = "This is some text "
.SelectAll()
.SelectionFont = New Font("Tahoma", 12, FontStyle.Bold)
.SelectionColor = Color.Chartreuse
I = .SelectionLength
S = "This is more text"
J = Len(S)
.Text &= S
.Select(I, J)
.SelectionFont = New Font("Arial", 8, FontStyle.Italic)
.SelectionColor = Color.Blue
.Select(0, 0)
End With

Aye, you've done one way what I've done another way. I don't know how many
times I missed the line that says that if you change the color / font and
then add to the rtb that it will then use that font / color. So My code
now looks like:
rtbList.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, FontStyle.Bold)
rtbList.AppendText(alWork.item1 & vbCrLf)
rtbList.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, FontStyle.Regular)
which is stupidly simple.....

Thanks to all who replied. //al
 
Back
Top