Click here to Skip to main content
15,889,281 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

 
GeneralRe: Rich Text Pin
Chrisi47626-Mar-07 2:07
Chrisi47626-Mar-07 2:07 
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 
First of all, even though you think your design is not so good, it is the fastest.

Mind you, even leaving along the coloring, we don’t have even satisfactory edit control, for .NET or native Windows. Window’s EDIT lacks important events (like cursor position changed) and also has poor performance; Rich Edit has several severe glitches; some of them are only manifested on big files (I’ve developed my own rich-edit based editor and had a hard time to find some work-arounds).

As to the #developer’s edit control, for some applications it is unacceptable, because it renders way too slowly when the file size is about 2M or more.

So, I would encourage you to work your control in a comprehensive lightweight component. First of all, would you please some of your bugs? Here are some I found (and fixed one of them):

1) You’re missing KeyDown events Ctrl+Ins (copy), Shift+Del (cut), Shift+Ins (paste), “old” CIU-style equivalents (actually, more convenient) of Ctrl+C, Ctrl+X, Ctrl+V.
2) PgUp(PgDown) works incorrectly: it just scrolls, but it MUST also change caret position.
3) RenderMode.GDINative does not work with Unicode; shame on you: working with .NET and P/Invoke you should not forget using “Wide” version of Windows API names and proper marshalling for the.NET type “string”. Please see my fix below but look carefully: generally, you’re supposed to add “W” to all API names.
4) Another RenderMode.GDINative problem: text background color differs from that of control – it’s some close “rounded-up” color.

My fix of bug (3): file Win32Wrapper.cs:
<br />
        [DllImport("gdi32.dll", EntryPoint = "ExtTextOutW")]<br />
        internal static extern bool ExtTextOut(IntPtr hdc, int X, int Y, uint fuOptions,<br />
            [In] ref RECT lprc,<br />
            [MarshalAs(UnmanagedType.LPWStr)]<br />
            string lpString,<br />
            uint cbCount, int [] lpDx);<br />
<br />
        [DllImport("gdi32.dll", EntryPoint="TextOutW")]<br />
        internal static extern bool TextOut(IntPtr hdc, int nXStart, int nYStart,<br />
           [MarshalAs(UnmanagedType.LPWStr)]<br />
           string lpString,<br />
           int cbString);<br />
<br />
        [DllImport("gdi32.dll", EntryPoint="GetTextExtentPointW")]<br />
        internal static extern bool GetTextExtentPoint(IntPtr hdc,<br />
           [MarshalAs(UnmanagedType.LPWStr)]<br />
           string lpString,<br />
           int cbString, ref Size lpSize);<br />



Design consideration: caret info area as a part of control is not appropriate. Instead, you should provide just a public event. The user most likely will process this event by updating status bar or something like that.

Lack of feature: how about Word Wrap (#developer’s editor does not have it either)?

Thank you.
--SA
Sergey A Kryukov

SAKryukov

GeneralRe: It's fast! but please fix the bugs Pin
Chrisi47617-Mar-07 5:26
Chrisi47617-Mar-07 5:26 
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.