Results 1 to 6 of 6

Thread: RichTextBox - deselect SelectionBackColor

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2019
    Posts
    40

    RichTextBox - deselect SelectionBackColor

    I've searched and seen lots of info on this but no solution, yet. Very possibly because i'm looking at it wrong. It seems simple enough an issue but I can't resolve.

    I have a RichTextBox:
    • The background (paper) colour can be changed. Say we have it black (i know how to do this)
    • User has the option to highlight certain words by selecting them, say in yellow like a highlighter pen (using .SelectionBackColor)
    • Sometime later maybe they want to un-highlight the selected text (i don't know how to do this, here is my issue!)


    • If I set it to Color.Transparent then it becomes white, not the black as we have the background.
    • If I set it to Color.Black as is the background then change the background to, say, Cyan then the text background is still black.


    There is other text in there (that i've not set a back colour to) that has a 'transparent' background and happily takes the paper colour as whatever i set it to. My question is, how do i revert a previously set backcolour to 'nothing' - i.e. turn it off?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,428

    Re: RichTextBox - deselect SelectionBackColor

    Don't set it to a specific color... set it to the background color... ..SelectionBackColor = .BackColor ...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2019
    Posts
    40

    Re: RichTextBox - deselect SelectionBackColor

    Tried that, but if I set it to .BlackColor then change the background to, say, Cyan then the text background is still black. I'm trying to set it, for want of a better phrase, to 'nothing'.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,682

    Re: RichTextBox - deselect SelectionBackColor

    In that case I think you'll need to do something more complex, and edit the .RTF itself.

    If you look at the contents of it after highlighting something, you will see it contains (among other things), something similar to this:
    ... some text \cf3 the highlighted text \cf0 some more text ...
    The one that has the number 0 is resetting to the default colour, and assuming the text before the first tag (in this example the one with 3) also has the default colour, simply removing both tags (so it is just "... some text the highlighted text some more text ...") will do what you want.

    However that is awkward to work out (because you will need to look back at previous tags until the start of the actual document after the header, rather than the start of the .RTF), so a simpler fix is to ensure they are both the default, so in this example set both tags to \cf0 . This will increase the file size a little, but makes the code much simpler to write.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2019
    Posts
    40

    Re: RichTextBox - deselect SelectionBackColor

    Understand. Thanks to all for your help.

  6. #6
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: RichTextBox - deselect SelectionBackColor

    I know this is an old post but I had the same issue, and came up with a solution. So I wanted to share in case someone else with this problem found this thread, or maybe wanted to make my solution better.

    First the issue: Once you have highlighted text using the methods available, the RGB values of the color are added to the "\colortbl" tag, a new "\highlightX" tag is added at the start of the highlight and a second "\highlightX" is added at the end. The second one will be "\highlight0" if it is returning to default, or some other number if it is a different color.
    Code:
    {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    {\colortbl ;\red0\green255\blue0;}
    {\*\generator Riched20 10.0.18362}\viewkind4\uc1 
    \pard\f0\fs17 Hello \highlight1 World. \highlight0 This is fun.\par
    }
    Unfortunately, no matter how you try to change the SelectionBackColor using the existing .Net methods, they ALL only update the color in the "\colortbl" to a fixed RGB color while leaving the "\highlightX" tags in place.

    This Sub will look at the selection and remove theappropriate "\highlightX" tags and leave the ones necessary to keep highlight on unselected text.
    Code:
        Private Sub ClearHighlight(ByVal rtbx As RichTextBox)
            Dim tmpStr As String = rtbx.SelectedRtf
            Dim matches As MatchCollection = Regex.Matches(tmpStr, "\\highlight(\d+)")
            Dim nextchar As String
            For x As Integer = matches.Count - 1 To 0 Step -1
                nextchar = rtbx.SelectedRtf.Substring(matches(x).Index + matches(x).Length, 1)
                If nextchar = " " Then
                    tmpStr = tmpStr.Remove(matches(x).Index, matches(x).Length + 1)
                Else
                    tmpStr = tmpStr.Remove(matches(x).Index, matches(x).Length)
                End If
            Next
            rtbx.SelectedRtf = tmpStr
        End Sub
    To use it, just Select the text you want to un-highlight, call the Method and pass in the RichTextBox Control:
    Code:
       ClearHighlight(RichTextBox1)
    You will also need to add the following line at the top of your code above the Class line.
    Code:
    Imports System.Text.RegularExpressions
    Last edited by Maverickz; Apr 2nd, 2021 at 05:29 PM.

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