Results 1 to 6 of 6

Thread: Help with Rich Text Boxes

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    42

    Resolved 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:
    1. Private Sub WriteStuff(ByVal stuffToWrite As String)
    2.  
    3. rtxMain.Text = stuffToWrite
    4.  
    5. End Sub

    And later I would call that procedure:

    VB Code:
    1. 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.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    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]

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    42

    Re: Help with Rich Text Boxes

    Quote Originally Posted by plenderj
    I would start by creating some overloads, and then perhaps insert raw RTF text into the textbox?
    *** does that mean?

  4. #4
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    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:
    1. Private Sub WriteStuff(ByVal stuffToWrite As String)
    2.         RichTextBox1.Text = stuffToWrite
    3.     End Sub
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         WriteStuff("Hello, world")
    7.     End Sub
    8.  
    9.     Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click ' Shows the Font menu
    10.         If Me.FontDialog1.ShowDialog = DialogResult.OK Then
    11.             RichTextBox1.SelectionFont = FontDialog1.Font
    12.         End If
    13.     End Sub
    you need to select of course the text you want to change its fontstyle or whatever.

  5. #5
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    42

    Thumbs up Re: Help with Rich Text Boxes

    Thanks, that's awesome!

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