Introduction

This DLL extends the RichTextBox control in Windows Forms to add background highlighting color, background color clearing, highlighting and 'Scroll To Bottom' support. The article on CodeProject by Frederick Volking entitled "RichTextBox with background highlighting in VB.NET" does pretty much the same as this, only his article edits the color tables and RTF text currently used in the RichTextBox. My version uses calls on the RichTextBox directly to highlight/clear selections.

Functionality Added

A stonking 4 (count them) methods are added to the RichTextBox:

RichTextBox.SelectionBackColor (Property) - Gets or sets the current selection's backcolor. Used the same way as RichTextBox.SelectionColor.

RichTextBox.ClearBackColor (Sub) - Clears all or just the selected backcolor as though it was never there.

RichTextBox.Highlight (Sub) - Highlights the given text in the RichTextBox with the given color.

RichTextBox.ScrollToBottom (Sub) - Scrolls the RichTextBox to the bottom of the viewport.

Reusing the New RichTextBox

The release version of the dll is included in the demo project, simply reference this DLL and add the DLL to the toolbox, then drop the new RichTextBox onto a form, then you are all done.

Share

About the Author

Self taught programmer of 6 years or so, started on the ZX Spectrum, through to the Amiga and then the PC. Enjoy coding for fun, most of my work is done in VB.NET, although have a background in many other langauges. Enjoy fiddling with Win32 and writing the odd small game here and there.

Currently studying a BSc(Hons) Software Engineering course at University.

Comments and Discussions

I´m using your richtextbox in a final exam for the university, and I´m reading the font name with 'FN (string) = rtbTexto.SelectionFont.Name', and works right!
But... when I select more than ONE name of font at the same time, Visual goes in an unhandled exception (sorry for my english) .

How can I append text to the richtextbox and keep the previous highligting. Currently, if I add any text, the entire background color the richtextbox will take on the highligting color? Any ideas on how to keep the current highlighted keywords and append text?

I have been struggling with the RichTextBox for about three days now and have been met with only frustration. My application is a terminal emulator that requires text coloring and highlighting for some of the terminal types. I dropped the project right into my solution in source control, added a reference, changed my System.Windows.Forms.RichTextBox to Tarro.Windows.Forms.RichTextBox and my struggles with highlighting text were instantly over. This freed me up to concentrate on the actual core of the application rather than fight with the standard RichTextBox. I'm giving it 5 stars!

-- modified at 3:47 Tuesday 9th May, 2006
One thing I forgot to mention. There is a small bug with the ScrollToBottom function. It seems to clip the bottom line. I tried resizing my textbox thinking that I could affect the size of the offset, but it always seemed to have the same offset. The scroll bar indicates it hasn't scrolled to the bottom as well since it shows a small amount of space between the thumb bar and the bottom scroll button.

- Who can tell me the CharFormat2 Structure : Private Structure CharFormat2
Public cbSize As Int32
Public dwMask As Int32 'this is mean ???
Public dwEffects As Int32 'This is mean ???
Public yHeight As Int32
Public yOffset As Int32 'This is mean ???
Public crTextColor As Int32 'This is mean ???
Public bCharSet As Byte 'This is mean ???
Public bPitchAndFamily As Byte 'This is mean ??? Public szFaceName As String
Public wWeight As Int16
Public sSpacing As Int16
Public crBackColor As Int32
Public lcid As Int32
Public dwReserved As Int32
Public sStyle As Int16
Public wKerning As Int16
Public bUnderlineType As Byte
Public bAnimation As Byte
Public bRevAuthor As Byte
Public bReserved1 As Byte
End Structure
for all.
- How can I get a explanation for SendMessage's functions. I found out a lot of SendMessage function but I don't know how do I do ???
- EveryBody can help me, thanks you !!!!

If you have got(or you are getting) the new express Visual C++ 2005 Edition, you will find that your code works really easily in that version. Because the new language is a mixture of Visual Basic and VS.Net, your code is ideal for highlighting text. The following example is for a RichTextBox (richTextBox1) on a windowsform. After you have highlighted some text and you have clicked a button, this is all the code that is needed:

Really nice code - easy to read and extend. I have a need for hiding some text as well, so I added a property SelectionHidden and a procedure UnHideAll(), in almost exactly the same way. Very easy to do (of course I can let you have the code if you like).

when i reference the dll in the c# project, the tarro class is working. but when i compile the project the error begin to occur.i dont really understand it. but i'm sure it is with the dll, and one more thing, when i use regasm to register the tarro dll. it cant be registered because of its depedencies. i really dont understand the error. can someone help me on how to answer the problem.thanks a lot

Another thing - If I just put an RTB on a form and run it, there are a few shortcut keys that work in it although I never set them, and there's property to turn it off (Ctrl-I to increase indentation, Ctrl-1,2,5 to change the line-spacing *no method to do it programtically??*).

"Command keys are keys that take precedence over regular input keys. Examples of command keys are shortcut keys and access keys. If this control has a shortcut menu (also known as a context menu), this method calls the processCmdKey method of the ContextMenu object that represents the shortcut menu to check for menu shortcuts. Finally, if the control has a parent, the key is passed to the parent's processCmdKey method. In this way, command keys are "bubbled" up the control hierarchy. "

I'm not really sure, I`ve not messed with command keys and such before very much, you could try posting about this on the main C# forums here on CP, you are more likely to get a response there
I`ll look into it a bit more too, see if I can find anything.