Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey,

 

I want to change the background color of the selected text in an rtb (not the WHOLE rtb background... just the background of the selection). I can't find any methods to do so in the C# library. Any ideas?

 

 

Thanks so much!

  • *Experts*
Posted

It's been a while since I dug into this so please bear with me. As I remember, you'll need to look into the RTF code and do some string manipulation to set the selected RTF backcolor. Here's the code that I adapted for setting and de-setting a highlight to the selected RTF.

 

   Private Sub Add_Highlight(rtf As RichTextBox)
       If rtf.SelectedText.Length <= 0 Then Exit Sub

       Try
           Dim strRTF As String = rtf.SelectedRtf
           Dim startpos As Integer = strRTF.IndexOf("\uc1") - 1
           Dim nextpos As Integer = strRTF.IndexOf("\pard\") + 6
           Dim strResult As String

           If strRTF.IndexOf("highlight1\") < 0 Then
               strResult = strRTF.Insert(nextpos, "highlight1\").Insert(startpos, HighlightBlackRtf(Color.Yellow))
               rtf.SelectedRtf = strResult
           Else 'if the text is already highlighted
               strResult = strRTF.Replace("{\colortbl;\red255\green255\blue0;}", "")
               strResult = strRTF.Replace("highlight1\", "")
               rtf.SelectedRtf = strResult
           End If

       Catch ex As Exception
           Console.WriteLine(ex.Message)
       End Try
   End Sub

   Private Function HighlightBlackRtf(ByVal c As Color) As String
       Dim strcol As String = "{\colortbl ;\red" & c.R & "\green" & c.G & "\blue" & c.B & ";}"
       Return strcol
   End Function

 

I hope this will help you.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...