Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSet formatting for a RichTextBox control all at once
DescriptionThis example shows how to set formatting for a RichTextBox control all at once in Visual Basic 6. It uses a CHARFORMAT structure to define the formatting and then sends the control the EM_SETCHARFORMAT message.
KeywordsRTF, Rich Text format, RichTextBox, RichText
CategoriesStrings, Controls
 
Thanks to Josh Einstein.

The program uses a CHARFORMAT structure to define the formatting. It then uses the SendMessage API function to send the RichTextBox the EM_SETCHARFORMAT message telling it to apply the formatting to the entire text.

This is faster and causes fewer side effects (such as flashing) than using SelStart, SelLength, SelBold, etc.

 
' Remove all character formatting except color.
Public Sub ClearCharacterFormat(TextObject As _
    RichTextLib.RichTextBox)
Dim udtCharFormat As CHARFORMAT    '// Structure to hold
    ' formatting info

    '// Initialize the structure's size parameter
    '// which tells windows that we want a CHARFORMAT
    '// structure and not a CHARFORMAT2 structure.
    udtCharFormat.cbSize = LenB(udtCharFormat)

    ' Define the attributes we will reset.
    udtCharFormat.dwMask = _
        CFM_BOLD Or _
        CFM_ITALIC Or _
        CFM_UNDERLINE Or _
        CFM_STRIKEOUT

    ' Set the attribute values.
    udtCharFormat.dwEffects = 0

    ' Set the attributes.
    SendMessage TextObject.hWnd, EM_SETCHARFORMAT, SCF_ALL, _
        udtCharFormat
End Sub
 
NOTE: In the original version of this program running on Windoes 2000, Josh had to declare the szFaceName field in the data structure as:

    szFaceName(LF_FACESIZE) As Integer

That makes each Unicode character take up 2 bytes. On my system, I used:

    szFaceName(LF_FACESIZE) As Byte
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated