Solved

How to change selection backcolor in a RichTextBox

Posted on 2002-06-28
6
1,155 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now