Click here to Skip to main content
15,889,403 members
Articles / Desktop Programming / Windows Forms
Article

ColorTextBox

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
25 Mar 2007Apache2 min read 149K   3.4K   81   50
This article describes ColorTextBox, a customizable User Control which was written completely from scratch and is intended to fill the gap between the TextBox and RichTextBox controls found in the .NET 2.0 library.

Screenshot - colortextbox_pic.png

Introduction

ColorTextBox is a .NET 2.0 User Control which was written from scratch and is meant as a replacement for the TextBox control of the .NET class library.

Background

ColorTextBox is a by-product of LMX-Editor, an Open Source XML editor I developed as part of my diploma thesis. Since I was very disappointed by the performance of the RichTextBox control available in the .NET library (the standard TextBox does not support colored text), I decided to write my own ColorTextBox on which the aforementioned editor is based.

Using the code

Although ColorTextBox does not inherit from the abstract TextBoxBase class of the .NET library, it was designed to be compatible with the default TextBox implementation. So, if you're familiar with the usage of the standard TextBox, you can use ColorTextBox right away.

In fact, it should be no or little effort to replace TextBoxes in existing applications with instances of the ColorTextBox. If you develop under Visual Studio .NET 2005, you can use the Forms Designer to configure all relevant properties of the control.

Just look at the code provided in the example: it's a simple text editor which was built in about 30 minutes, and shows the most important features of the ColorTextBox.

Points of interest

As mentioned before, the ColorTextBox control is the base for an Open Source XML editor I wrote, which includes features like syntax highlighting and folding. If you want to write your own code editing component, have a look at the complete source code at SourceForge, which contains a generic CodeTextBox control which can be easily extended to support the mentioned features. Furthermore, the latest code for ColorTextBox will always be available under the link above!

History

03-08-07 Version 1.0.0:

  • Initial release
03-12-07 Version 1.0.1:
  • Added various keystroke shortcuts
  • Support for arbitrary input chars
03-25-07 Version 1.1.0:
  • Various bugfixes (Color support, Rendering, Scrolling, ...)
  • Added support for additional shortcut keys (similar to TextPad)
  • Added properties to improve compatability with the TextBox class
  • Added new demo application to demonstrate the most important features
  • Note: Some interfaces had to be changed!

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralChanging color of text on screen Pin
sanong21-Mar-07 0:40
sanong21-Mar-07 0:40 
GeneralRe: Changing color of text on screen Pin
Chrisi47621-Mar-07 4:26
Chrisi47621-Mar-07 4:26 
GeneralRe: Changing color of text on screen [modified] Pin
Chrisi47621-Mar-07 8:11
Chrisi47621-Mar-07 8:11 
GeneralRe: Changing color of text on screen Pin
sanong21-Mar-07 16:49
sanong21-Mar-07 16:49 
GeneralRe: Changing color of text on screen Pin
Chrisi47625-Mar-07 11:44
Chrisi47625-Mar-07 11:44 
GeneralRe: Changing color of text on screen Pin
sanong26-Mar-07 6:49
sanong26-Mar-07 6:49 
GeneralIt's fast! but please fix the bugs Pin
Sergey Alexandrovich Kryukov16-Mar-07 9:54
mvaSergey Alexandrovich Kryukov16-Mar-07 9:54 
GeneralRe: It's fast! but please fix the bugs Pin
Chrisi47617-Mar-07 5:26
Chrisi47617-Mar-07 5:26 
First of all, thanks for the very constructive input! Concerning the bugs you mentioned:

1. KeyEvents:

Since I do not acutally use the control very much I didn't pay much attention to standard key event handling, but I'll try to implement the missing features as soon as possible!

2. PageUp / PageDown:

Same here: will be implemented in the next release!

3. GDINative rendering:

I implemented this rendering path to do some performance comparsions and I think it doesn't really offer great benefits in that area (maybe it does on slower machines, but I do not have the possibility to verify this as I do not own a sub-GHz PC anymore). Besides the bugs you pointed out (I wasn't aware of the Unicode issue - thanks) there are still other problems involved with native GDI rendering. A long story short: it simply doesn't work the way it's supposed to and should currently not be used. I'll add a comment to the enum value to point that out until I have the time to fix it (of course I'll add the fix you provided to the next release, but as I said there are still some other issues to solve!)

4. Background color for GDINative rendering:

See point 3.

Concerning the caret info area inside the control - let me put it the Microsoft way: "This behavior is by design."
In fact one major feature of the control is the support for reserved areas at the edges of the text area which allow easy extensibility in subclasses. Let me copy / paste the description of that feature from the source code:

/* Reserved areas are regions that can be added to the four sides of the client area for user
 * defined painting. The line rendering will always be limited to the area surrounded by all
 * reserved areas that have been added to the Control. To add a reserved area the width or height
 * of the area, the location of the area and a method responsible for drawing the area have to be
 * supplied. Reserved areas need to have a unique identifier string for a given location and can be
 * inserted at a given index (starting with 0 for the innermost area)
 * For clarity the follwing graphic shows the concept for a ColorTextBox with 1 top, 2 left,
 * 1 right and 2 bottom areas:
 *
 *       _________________________
 *      |____________0____________|
 *      | | |                   | |
 *      | | |                   | |
 *      |1|0|     Text Area     |0|
 *      | | |                   | |
 *      |_|_|___________________|_|
 *      |____________0____________|
 *      |____________1____________|
 *
 *
 * NOTE: It's not recommended to put a lot of complex logic inside the registered draw method of a
 * reserved area as this can slow down the whole rendering process significantly!
 */


So if you do not want to show the caret info area you can simply disable it with the respective property. Additionally I will add a public CaretChanged event to support external handling of caret events (the way you suggested).
I hope this helps!

Regards,

Christian
GeneralRe: It's fast! but please fix the bugs Pin
Sergey Alexandrovich Kryukov19-Mar-07 11:10
mvaSergey Alexandrovich Kryukov19-Mar-07 11:10 
GeneralRe: It's fast! but please fix the bugs Pin
Chrisi47621-Mar-07 8:30
Chrisi47621-Mar-07 8:30 
GeneralCannot enter Polish letters. Pin
vanad.min13-Mar-07 9:35
vanad.min13-Mar-07 9:35 
GeneralRe: Cannot enter Polish letters. Pin
Chrisi47614-Mar-07 13:11
Chrisi47614-Mar-07 13:11 
QuestionWordwrap? Pin
Quentin Pouplard8-Mar-07 23:13
Quentin Pouplard8-Mar-07 23:13 
AnswerRe: Wordwrap? Pin
Chrisi4769-Mar-07 6:14
Chrisi4769-Mar-07 6:14 
QuestionRe: Wordwrap? Pin
lalaphilippe16-Nov-11 5:46
lalaphilippe16-Nov-11 5:46 
QuestionSelect All (CTRL + A) and other Keys Shortcuts not supported (or Bug) ? Pin
Miketrix8-Mar-07 21:12
Miketrix8-Mar-07 21:12 
AnswerRe: Select All (CTRL + A) and other Keys Shortcuts not supported (or Bug) ? Pin
Chrisi4769-Mar-07 6:07
Chrisi4769-Mar-07 6:07 
GeneralRe: Select All (CTRL + A) and other Keys Shortcuts not supported (or Bug) ? Pin
emty2229-Feb-08 14:16
emty2229-Feb-08 14:16 
GeneralKey for the source code on sourceforge Pin
uniquekaiser8-Mar-07 11:01
uniquekaiser8-Mar-07 11:01 
GeneralRe: Key for the source code on sourceforge Pin
Chrisi4769-Mar-07 6:01
Chrisi4769-Mar-07 6:01 
Generalwritten from scratch Pin
Ian MacLean8-Mar-07 10:59
Ian MacLean8-Mar-07 10:59 
GeneralRe: written from scratch Pin
Chrisi4769-Mar-07 5:41
Chrisi4769-Mar-07 5:41 
Generaljunk [modified] Pin
Thanks for all the fish8-Mar-07 9:57
Thanks for all the fish8-Mar-07 9:57 
GeneralRe: junk Pin
toxcct12-Mar-07 22:06
toxcct12-Mar-07 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.