0

It's simple to change the back color of the entire richtextbox but not on a single word or character, is there a API or user control that happens to do that. I tried saving a word doc as rtf and copied the item and pasted it in the rich text box and it worked but it doesn't work so well, how can this be done. Hard or easy!

1 Answers1

0

I once needed to highlight text in RichTextBox for a VB6 project along time ago.. and I used the following module (not written by me)

Module:

Option Explicit

Public Const LF_FACESIZE = 32
Public Const WM_USER = &H400
Public Const EM_SETCHARFORMAT = (WM_USER + 68)
Public Const CFM_BACKCOLOR = &H4000000
Public Const SCF_SELECTION = &H1

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Type CHARFORMAT2
    cbSize As Integer    '2
    wPad1 As Integer    '4
    dwMask As Long    '8
    dwEffects As Long    '12
    yHeight As Long    '16
    yOffset As Long    '20
    crTextColor As Long    '24
    bCharSet As Byte    '25
    bPitchAndFamily As Byte    '26
    szFaceName(0 To LF_FACESIZE - 1) As Byte    ' 58
    wPad2 As Integer    ' 60

' Additional stuff supported by RICHEDIT20
    wWeight As Integer    ' /* Font weight (LOGFONT value)      */
    sSpacing As Integer    ' /* Amount to space between letters  */
    crBackColor As Long    ' /* Background color                 */
    lLCID As Long    ' /* Locale ID                        */
    dwReserved As Long    ' /* Reserved. Must be 0              */
    sStyle As Integer    ' /* Style handle                     */
    wKerning As Integer    ' /* Twip size above which to kern char pair*/
    bUnderlineType As Byte    ' /* Underline type                   */
    bAnimation As Byte    ' /* Animated text like marching ants */
    bRevAuthor As Byte    ' /* Revision author index            */
    bReserved1 As Byte
End Type

Call:

to highlight the selected text with yellow color

Dim RTFformat As CHARFORMAT2
With RTFformat
    .cbSize = Len(RTFformat)
    .dwMask = CFM_BACKCOLOR
    .crBackColor = vbYellow
End With
SendMessage RichTextBox1.hwnd, EM_SETCHARFORMAT, SCF_SELECTION, RTFformat

Hope this helps :)

  • 1
    Tried your code but it remains a normal text with no highlight, could be I did it wrong I just selected richtextbox text and clicked on command button with the code to call it no error just stayed the same, have any idea? –  Jan 24 '16 at 18:04
  • 1
    I just tested it again, and it works perfectly fine.. you might download and run this test project => check if it works on your machine: https://goo.gl/ZgQODv – 41686d6564 stands w. Palestine Jan 24 '16 at 18:25
  • 1
    What OS are you running might have to do with that I use windows 2007? –  Jan 24 '16 at 20:09
  • 1
    You probably mean 'Windows 7' or maybe 'Windows server 2008', anyway I tested this on both (x64 versions) and it worked perfectly.. did you try the sample project that I uploaded for you? – 41686d6564 stands w. Palestine Jan 25 '16 at 09:49
  • 1
    I tried your upload and it doesn't do anything, it has the sentence about the fox... but when I click to highlight it remains the same! It's puzzling. –  Jan 25 '16 at 16:38
  • 1
    I also searched the web and found stuff that are supposed to do functions like yours but it doesn't do anything either, this is another problem I was having with the richtextbox to insert a table into the text box but it just inserted words and not the table, do you think that perhaps there is more then one version of the richtextbox control and I am using the wrong one? It would then make sense why I am unable to do all these things with the richtextbox. –  Jan 25 '16 at 18:00
  • 1
    I don't think the problem is relative to the Richtx32 version.. anyway, this is the version that I'm using: https://goo.gl/Hz9fJC you can try to replace your existing file in System32 (or SysWOW64) with this one, and check if that makes any difference – 41686d6564 stands w. Palestine Jan 25 '16 at 18:43
  • 1
    Thanks for your version, same result so I guess that the problems somewhere else I just have no idea where! I also tried where it says .crBackColor = vbYellow to change it to .crBackColor = vbBlue in case it had to do with the screen I have not showing yellow but it still did nothing, I have no idea why this is not working by me! –  Jan 25 '16 at 21:55
  • The code works now! It's strange that it didn't before, thanks for your help. –  Jan 27 '16 at 14:22
  • I'm glad I was able to help :) – 41686d6564 stands w. Palestine Jan 27 '16 at 14:49