How to set the selection fontstyle of a richtextbox?

Erikvandervelde

Newcomer
Joined
May 23, 2003
Messages
11
Hi there!

I'm trying to write a kind of simple textwriter in VB.NET
In visual basic 6, this whas quite easy!
But in VB.NET i noticed that the bold property of the selectionfont is readony...

I know I can set the font like this:

Dim Myfont As New Font("Tahoma", FontStyle.Bold)

But in this case, if the font was underlined as well, the underline is gone! :(

So I have to check, all the applied font-styles, before i could set the font! :confused:

I wrote some code, which processed 1 character at a time.
That works, but very, very slow!

Does anyone knows some code, to help me out???

Thanks!

Greets,

Erik van der Velden.
Holland
 
Visual Basic:
        With rtfChannel
            .SelectionStart = Len(.Text)
            .SelectionColor = System.Drawing.Color.SteelBlue
            .SelectedText = inData
        End With
the colour bit being this ...
rtfChannel.SelectionColor = System.Drawing.Color.SteelBlue
rtfChannel being the richtextbox.:)

oops and the Font ?
Visual Basic:
rtfChannel.SelectionFont = New System.Drawing.Font("Tahoma", 10.0!)
 
almost forgot the Bold bit.
Visual Basic:
rtfChannel.SelectionFont = New System.Drawing.Font(read_font, 10.0!, FontStyle.Bold)
'// read_font is where your font would be btw.
hope this helps , the richtextbox control is quite good in .net once you get used to it :)
 
Here's how to make the selection bold but keep any existing style it has (italic, underline etc):

Visual Basic:
rtb.SelectionFont = New Font(rtb.SelectionFont.Name, rtb.SelectionFont.Style Or FontStyle.Bold)
 
Thank you very much for you reply's!

But I'm still having problems!

When the selectedtext in the richtextbox contains multiple fonts (or fontstyles (size, bold, undelined, italic or whatever...)) the Selectionfont property returns 'Nothing'. So I have to read until something in the font changes (that's a lot to check!) and then set the text to far, etcetera....
This works, but very slow! Especialy when you select multiple lines with different fonts or fontstyles!
I'm thinking about sending windows messages to the richtextbox.
Does anyone has expierence?

Hoping to hear from you!

Greets,

Erik van der Velden.
 
are you receiving the multiple fonts / colours in to the richtextbox from another source ( like with irc , where you have to sort the correct colours for the different chars )?
goede middag:)
 
Goede middag! Very good!

I'm not sure I understand what you mean.
I am reading a RTF file from disk.
My problem is (for example...) that when I have a word which contains 2 different fonts, the first 3 chars are bold, and the second 3 are underlined.
Now I select the complete word en want to make the complete word Italic AS WELL (without loosing the fontstyle per char!)
But now I have to fontstyle for the complete word!
In VB6 you should say: MyRtfBox.SelectionItalic = True
This will make the word Italic as well, but also it's current state! (Bold or Undelined and different font!)
How should I do this with the richtextbox in VB.NET?

Thanks again!

Goede middag!
 
if you are reading RTF from a disk , why not use richtextbox.SelectedRtf ? because if it's rtf in the first place you shouldnt need to decode for the different fonts / colours :-\
eg:
Visual Basic:
rtb.SelectedRtf = "myrtf file"
thats if it's actually rtf format you are reading :)
 
Once i have read the Rtf from the file... THEN i want to change the fontstyle of some words.
Divil, thanks for the link to the article.
I will try it tomorrow.
As you can read in my first message, i already tried this myself.
It worked, but very slow. Perhaps is this code better!

Thanks a lot all of you!

Erik.
 
Back
Top