Solved

How to change selection backcolor in a RichTextBox

Posted on 2002-06-28
6
1,163 Views
Last Modified: 2007-12-19
Is there a way to change the background color of selected text in a RichTextBox? There is the SelectionColor property, but it's the forecolor. The BackColor property changes the whole control.

I want something like the "Highlight text" feature of Microsoft Word, for VB.NET please.

Thanks.
0
Comment
Question by:ricardog2
  • 3
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
CJ_S earned 100 total points
ID: 7118264
You will need to work with selectedrtf

RichTextBox1.SelectedRtf = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}{\colortbl ;\red255\green0\blue0;\red255\green0\blue255;}\uc1\pard\cf1\highlight2\f0\fs24 Mytext}"

It's pretty hard to get a grasp on though, but it will work once you get to know it well. i might have time available to do this automatically, but it'll take some time (and I'm leaving now)

CJ
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7118265
Easiest is to make it in Word -> copy to rtf field -> select that part -> msgbox(richtextbox.selectertf)

then copy the rtf code and use it to your likings.

CJ
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7119014
If you find no direct solution looking at the RTF codes generated by Word, take a look at this ocx with full source that implements this functionality via the rich edit library:

http://www.vbaccelerator.com/codelib/richedit/richedit.htm
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 1

Author Comment

by:ricardog2
ID: 7119371
The first solution seems to be more flexible, because that custom control needs a lot of dependency files and it's written in VB6. The VB.NET RichTextBox class has everything I need except the selection backcolor.

I'll look at the RTF Specification to understand that RTF code better.

Thank you for your replies.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7120059
I'm not sure how the .NET RichTextBox has been altered, but if it still is a wrapper around the riched*.dll then it may not support the RTF codes that specify selection backcolor.  That was an issue on several items, for me, when working with the VB6 richtextbox.  Things that were valid RTF codes, were not supported in the component (e.g., header/footer, page breaks, etc.)
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7120172
Azra,
Good to know!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question