|
-
Feb 11th, 2005, 07:03 AM
#1
Thread Starter
Member
Help with Rich Text Boxes
I have a really big Rich Text Box control on my form, named rtxMain. There are several procedures in my code that write text to this text box. Here is the simplest one:
VB Code:
Private Sub WriteStuff(ByVal stuffToWrite As String)
rtxMain.Text = stuffToWrite
End Sub
And later I would call that procedure:
VB Code:
WriteStuff("Hello, World!")
My question is, how do I write rich text (bold text, italic text, colorful text, etc.) to this text box, where I only want part of the text string to have special formatting? Like, if I ONLY wanted the "World" part to be bold, and the rest normal, how would I do this?
(of course that's not really what i'm writing, that's just an example)
Last edited by Coolness; Feb 12th, 2005 at 09:52 PM.
-
Feb 11th, 2005, 07:11 AM
#2
Retired VBF Adm1nistrator
Re: Help with Rich Text Boxes
I would start by creating some overloads, and then perhaps insert raw RTF text into the textbox?
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 11th, 2005, 07:24 AM
#3
Thread Starter
Member
Re: Help with Rich Text Boxes
 Originally Posted by plenderj
I would start by creating some overloads, and then perhaps insert raw RTF text into the textbox?
*** does that mean?
-
Feb 11th, 2005, 10:14 PM
#4
Hyperactive Member
Re: Help with Rich Text Boxes
for me i need some context menu apply on it and a FontDialog in the richtextbox control, but i think there's another...another better way to do it just dunno...
VB Code:
Private Sub WriteStuff(ByVal stuffToWrite As String)
RichTextBox1.Text = stuffToWrite
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WriteStuff("Hello, world")
End Sub
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click ' Shows the Font menu
If Me.FontDialog1.ShowDialog = DialogResult.OK Then
RichTextBox1.SelectionFont = FontDialog1.Font
End If
End Sub
you need to select of course the text you want to change its fontstyle or whatever.
-
Feb 12th, 2005, 01:21 PM
#5
Re: Help with Rich Text Boxes
Hi,
The RichTextBox (RTB) uses the property .Text to set strings and .RTF to set formatted text. Both of these apply to the entire RTB. IF you want to just apply to selected text, use the .SelectedText and .SelectedRTF properties instead.
If you can actually get hold of the formatted text, from another RTB for example, then you can set it using these.
Changing the formatting of text that is already in the RTB is a different kettle of fish (KOF ). There used to be handy VB6 properties to deal with formatting called SelBold, SelItalic etc. These have all been done away with in favour of the Font (or SelectionFont) object, and it is one of the most irritating changes in the world.
The Font object has a variety of properties, .Bold etc, but they're all readonly. The only thing that is not readonly is the object itself. The way to change font attributes is to set the Font (or Selectionfont) object to be the same as another font object. This you can do by getting it from somewhere else, eg as fret has done from a dialog, or by dimensioning a new one:
Dim newfont1 as New font("Times New Roman", "12", 5)
RTB1.selectionfont = newfont1
Here, the font properties are Font([Name], [Size], [style]), but there are a variety of different combinations you can use. You'll see if you try it, or look up Font object in the help.
So you might want, for example:
Writestuff("Hello World")
RTB1.selectionstart = 6
RTB1.selectionlength = 5
[there are other ways to select a portion of text in an RTB]
Dim newfont1 as New font("Times New Roman", "12", 5)
RTB1.selectionfont = newfont1
HTH
zaza
-
Feb 12th, 2005, 09:29 PM
#6
Thread Starter
Member
Re: Help with Rich Text Boxes
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|