Click here to Skip to main content
15,889,096 members
Articles / Programming Languages / C#
Article

Advanced Text Editor with Ruler

Rate me:
Please Sign up or sign in to vote.
4.85/5 (47 votes)
20 Mar 2008CPOL3 min read 431.3K   32.4K   164   134
Extending RichTextBox with ruler and much more
AdvTextEdit.PNG

Introduction

About two years ago I was creating a project for university. It was a complex of five programs that were related to testing (checking knowledge I mean). One of them contained a text editor. But unfortunately, I didn't know how to do that, that's why it was very simple. So, after these two years, I decided to rewrite my program in C# .NET (initially, it was written in VB.NET) and create a new powerful text editor. You can see part of it here.

Background

I spent a lot of time(!) searching the Internet for controls like this, but the best ones are shareware and others did not fit my needs. So, what does this control have? I tried to make it look like Microsoft Word and I think that there are some similarities. The ruler lets you change the following: left and right margins, left indent, hanging indent and right indent. You also can disable margins (their values are set to 1). You can see how it looks in the picture above.

Also, you can add tabs by clicking on the control with the left mouse button. But it is allowed only inside the area bounded by margins. If you want to remove a tab, just drag it off from the control.

The editor lets you use lists, underline styles, advanced char styles (you can create your own links, that are not words starting with "http://" or even "www"), OLE functionality is also available. I want to thank Oscar Londoño for his article Inserting Images into a RichTextBox Control (The OLE Way). It helped me a lot to deal with OLE. This project contains his code.

Using the Code

You can use the code according to the CPOL.

Projects are created as Windows Applications, but you can easily convert them into *.dll or just embed code into your project.

Unfortunately, I removed Visual Studio 2005, so, sorry but I can't create and upload a Visual Studio 2005 project. But you can import all required files into a Visual Studio 2005 project without any problems. However, note that Visual Studio 2008 added some new namespaces (like LINQ) that Visual Studio 2005 does not "understand". Just remove them. That's all. You are ready.

Points of Interest

One thing that shocked me is that Microsoft has released RichTextBox 6.0! (It is distributed with Microsoft Office 2007) but... with one exception. There is no documentation about its features. All that I've found is a list of added functions. You can find it here. There are also descriptions for all released versions of RichTextBox.

History

  • 7 January 2008: Initial release
  • 16 January 2008: Projects for Visual Studio 2005 and 2008 posted instead of standalone control.
  • 26 January 2008: Posted completed AdvancedTextEditor project. It is available for Visual Studio 2005 and Visual Studio 2008.
  • 10 February 2008: Fixed PrintDialog bug. It didn't receive focus when shown. Now it's OK. Thanks to Chris Schucker for bug report and recommendations.
  • 22 February 2008: Added new functionality (full text justification, underline styles and colors and other)
  • 24 February 2008: Fixed bug with conversion from millimeter to pixel and vice versa Thanks to Chris Schucker. Also corrected bug which caused incorrect display of the indents.
  • 19 March 2008: Version 2.0 released. A lot of changes including lists, OLE, underline styles, etc.

Additions

I understand that I am not able to include everything into the control, that's why you can freely add something useful or change code to fit your needs. But, please inform me about that, just email me at krasssss@mail.ru. It's just to let anyone use an upgraded (and corrected) version of that control. Of course, advisors will be mentioned.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Kazakstan Kazakstan
Currently I am studying at East Kazakhstan State Technical University. My future occupation is engineer-programmer.

I use Visual Basic, C#, Delphi and a little C++. Also I am interested in using SQL and Perl.

Comments and Discussions

 
GeneralRe: Something in new version [modified] Pin
Aleksei Karimov21-Mar-08 1:01
Aleksei Karimov21-Mar-08 1:01 
GeneralRe: Something in new version Pin
minhvc23-Mar-08 16:04
minhvc23-Mar-08 16:04 
GeneralRe: Something in new version Pin
Aleksei Karimov25-Mar-08 14:49
Aleksei Karimov25-Mar-08 14:49 
GeneralAbout Page Borders [modified] Pin
Rajkhan from Chandigarh14-Mar-08 1:30
Rajkhan from Chandigarh14-Mar-08 1:30 
GeneralRe: About Page Borders Pin
Aleksei Karimov14-Mar-08 6:15
Aleksei Karimov14-Mar-08 6:15 
Generala few things I did with your code ... Pin
BillWoodruff8-Mar-08 19:57
professionalBillWoodruff8-Mar-08 19:57 
GeneralRe: a few things I did with your code ... Pin
Aleksei Karimov8-Mar-08 22:08
Aleksei Karimov8-Mar-08 22:08 
Generalthanks ! and a few feature suggestions Pin
BillWoodruff4-Mar-08 18:58
professionalBillWoodruff4-Mar-08 18:58 
Hi Aleksei,

Many thanks all your hard work on this and your quick responsiveness to suggsestions and bug reports !

A few suggestions :

1. when you insert a picture : an error is thrown in both VS2005 and VS 2008 versions : temporarily an image of the desktop or some other interface element appears; when you hit "continue" the inserted image appears correctly, however.

This error was observed directly running the .exe files in the /bin directory.

Revised comment : I am observing the above error only on my laptop computer now. Both my laptop and the desktop have all the latest versions and updates of all the versions of the .NET framework. The laptop is runnng XP home, and the desktop XP Pro.

2. ability to drag and drop a picture in would be great

3. ability to drag and drop an rtf file directly into the workarea and have it open would be great.

4. It might be worthwhile to maintain the current file directory pointer independent of where the user has last opened a picture to insert from

5. I've always wanted a program I could paste icon files into; guess I'm just going to have to write that code Smile | :)

One thing I am very interested in, but certainly don't expect you to implement Smile | :) is the ability to have small picture files as bookmarks in an rtf file and be able to track them so the user can "jump" to them via key-combinations or menu choice. Right now if you examine the code of an rtf file with multiple instances of the same small gif file, for example, you'll find no distinguishing information between one instance and another in the rtf internal structure; they're just the same metafile repeated. I probably should go and examine the rtf spec to see if there's any capability to attach a custom id to a given metafile, but the idea of consulting the rtf spec gives me cold shudders Smile | :)

Again, thanks for your sharing your mind and your work. You got my "five."

best, Bil Woodruff

"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis

GeneralRe: thanks ! and a few feature suggestions [modified] Pin
Aleksei Karimov8-Mar-08 19:37
Aleksei Karimov8-Mar-08 19:37 
Generala bug when insert image Pin
johndragon4-Mar-08 15:20
johndragon4-Mar-08 15:20 
GeneralRe: a bug when insert image Pin
Aleksei Karimov8-Mar-08 19:30
Aleksei Karimov8-Mar-08 19:30 
QuestionError when change Font Face Pin
minhvc3-Mar-08 23:23
minhvc3-Mar-08 23:23 
GeneralRe: Error when change Font Face Pin
Aleksei Karimov8-Mar-08 19:42
Aleksei Karimov8-Mar-08 19:42 
GeneralRe: Error when change Font Face Pin
minhvc9-Mar-08 16:00
minhvc9-Mar-08 16:00 
AnswerRe: Error when change Font Face Pin
Aleksei Karimov10-Mar-08 6:03
Aleksei Karimov10-Mar-08 6:03 
QuestionWhere are Icons you import in your project? Pin
minhvc2-Mar-08 15:53
minhvc2-Mar-08 15:53 
AnswerRe: Where are Icons you import in your project? [modified] Pin
Aleksei Karimov3-Mar-08 7:42
Aleksei Karimov3-Mar-08 7:42 
QuestionHow to use TOM ( Text Object Model) with RichText Control Pin
minhvc2-Mar-08 15:01
minhvc2-Mar-08 15:01 
AnswerRe: How to use TOM ( Text Object Model) with RichText Control Pin
Aleksei Karimov3-Mar-08 7:55
Aleksei Karimov3-Mar-08 7:55 
QuestionRe: How to use TOM ( Text Object Model) with RichText Control Pin
minhvc3-Mar-08 13:48
minhvc3-Mar-08 13:48 
AnswerRe: How to use TOM ( Text Object Model) with RichText Control Pin
Aleksei Karimov10-Mar-08 6:01
Aleksei Karimov10-Mar-08 6:01 
GeneralRe: How to use TOM ( Text Object Model) with RichText Control Pin
minhvc10-Mar-08 20:53
minhvc10-Mar-08 20:53 
AnswerRe: How to use TOM ( Text Object Model) with RichText Control Pin
Aleksei Karimov11-Mar-08 5:02
Aleksei Karimov11-Mar-08 5:02 
QuestionRe: How to use TOM ( Text Object Model) with RichText Control Pin
minhvc16-Mar-08 15:00
minhvc16-Mar-08 15:00 
AnswerRe: How to use TOM ( Text Object Model) with RichText Control Pin
Aleksei Karimov16-Mar-08 21:15
Aleksei Karimov16-Mar-08 21:15 

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.