Hello again, my friends. Today's control is a very simple yet useful control. It is a custom control that contains both a
RichTextBox and a
Toolbar. Now you might ask: "Why is that so important and useful?" It's because I have taken the liberty to do some coding for you. The toolbar not only looks pretty but it's functional too. It contains some of the everyday, plain, and simple things you want your users to be able to do when they input text.
[Cheesy flashback music and graphics.] It all started when I was trying to remember how to insert a rich text "stamp" into a
RichTextBox. Along the road to remembering, I thought to myself "Self, wouldn't it be easier not to have to remember this stuff every 6 months?" My answer was a big yes. It would be easier if I just built a control to use every time I needed users to input text. Then thinking of all the wonderful and helpful people on The Code Project, I decided to distribute my control so that everyone can learn how to do this stuff and also so they will have a pre-built control for use whenever they want.
Using the control
There are two ways you can use this control. You can either add the RichTextBoxExtended.cs file to your project and use the control by setting it up in code, or you can add the control to your toolbar and drag it on to your
Form. One other thing, if you add the .cs file to your project, you will also have to add an
ImageList to your project that contains all the images for the
Toolbar. I prefer (and suggest) the latter method because I get to see the control on the form and I don't have to create the
ImageList on every form I want to use this control on.
There are several properties that will help you use this control, and they are listed below:
Toolbar - only use at runtime, changes that are made at design time will not persist.
RichTextBox - only use at runtime, changes that are made at design time will not persist.
Out of all of these, most of them are simply turning features on and off. The only really special ones are
StampColor is what it sounds like. It is the color the text will be in when a stamp is added to the
StampAction can be one of three values:
EditBy is a string that reads "Edited by " +
CurrentPrincipal.Identity.Name + " (
theUserName) on " +
Custom, you must handle the stamp event of this control.
private void richTextBoxExtended1_Stamp(object sender, System.EventArgs e)
StringBuilder stamp = new StringBuilder("");
if(richTextBoxExtended1.RichTextBox.Text.Length > 0)
stamp.Append("Custom stamp goes here!\r\n");
richTextBoxExtended1.RichTextBox.SelectionLength = 0;
Points of Interest
The biggest point of interest after all these months now is the amount of feedback, interest, and work one little control can bring to your life. I haven't been able to maintain this code like I wanted but thanks to all that have helped and made suggestions along the way. Along with this updated article comes news of
RichTextBoxExtended for Framework 2.0. So if you want all these features in VS 2005 then keep watching. Anyone wanting to help, just email me.
OK, so what did I learn from this control and this experience? I learned about Exclusive-OR. This is a really handy operation that saved me 100 or so lines of code in this control. In the code below, the OR is the "
^". The best way I can explain it is, it takes two values like 00000001 and 00000010 and compares them. In the example below, it checks the style and if the style = 00000001, it will change it to 00000000, however if it is 00000000, then it will change it to 00000001. That isn't the best explanation of this operation, so below the code are some links for you.
rtb1.SelectionFont = new Font(rtb1.SelectionFont,
rtb1.SelectionFont.Style ^ FontStyle.Bold);
- 1.0 - Initial release - 1/31/2004.
- 1.1 - 4/18/2005.
- Font Selector added.
- Font Size Selector added.
- Fixed error that caused the control to crash when two or more font types were selected.
- Cleaned up the code.
- 1.2 - 12/12/2005
- Fixed multiple font selection bug. You can now add font styles (e.g. Bold and Color) when selecting two different fonts at the same time.
- Added a selection change event named
- Added cut, copy and paste buttons.
- The control now uses the
ToolBarButton.Tag property instead of the
ToolBarButton.ToolTip property for button presses. Now you can have your tooltips be whatever you like.
DetectUrls property. You can have HTTP links in your
RichTextBox and click them to open the system default browser.
- Added a
- Added keyboard handler for Ctrl+B, Ctrl+I, Ctrl+S, Ctrl+U, Ctrl+-.
- The download now comes with all the images. I also included a print image.
- Cleaned up the code.