![]() |
Desktop Development »
Edit Controls »
General
Intermediate
Syntax highlighting textbox written in C#By Uri GuyThis article describes the code of a RichTextBox that knows how to highlight parts of the text and provide auto-completion. |
C#.NET 1.1, Win2K, WinMobile2003VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Hey. This code is a class derived from .Net's RichTextBox. It offers syntax highlighting and auto-completion features (like Intellisense, only a little dumber).
Have you ever written an application that had its own script language? Ever needed to edit SQL/code in you application? Didn't you just want a control that was a little better than a TextBox with a fixed width font? Now you can have it :-)
This control was written when I needed to edit SQLs in an application I wrote. Then I thought:
"Even if I could find a control that knows to edit SQLs well, it still wouldn't highlight names of tables or stored procedures that I wrote. And I know I'll need a control that can highlight my own script language sometime..."
So I sat down and wrote it. Later I realized that syntax highlighting without an auto-completion is not worth much, so I added support for that too.
First of all, I must apologise. I'm not much of a GUI programmer, so all of the manipulation is done using code, as I lack the knowledge to write custom editors for a PropertyGrid. With that out of the way, we may continue! The control has two inputs for highlighting:
Seperators property.
Each separator is a char, and what is later refferd to as "token" is a not-empty string between separators.
HighlightDescriptor method to add a HighlightDescriptor.
A HighlightDescriptor is an instance of a class describing a highlighting rule, which can be divided into token identification info and design info.
A highlighting rule has 6 fields it's constructed with:
Due to laziness, a HighlightDescriptor's values can only be set in the constructor. <SideNote> I really think that the readonly keyword is under appreciated.</SideNote>.
The HighlightDescriptor is as follows:
public class HighlightDescriptor
{
public HighlightDescriptor(string token,
string closeToken, Color color, Font font,
DescriptorType descriptorType, DescriptorRecognition dr,
bool useForAutoComplete)
{
Color = color;
Font = font;
Token = token;
DescriptorType = descriptorType;
CloseToken = closeToken;
DescriptorRecognition = dr;
UseForAutoComplete = useForAutoComplete;
}
public readonly Color Color;
public readonly Font Font;
public readonly string Token;
public readonly string CloseToken;
public readonly DescriptorType DescriptorType;
public readonly DescriptorRecognition DescriptorRecognition;
public readonly bool UseForAutoComplete;
}
There were a few interesting things during the writing:
EM_GETSCROLLPOS and EM_GETSCROLLPOS Windows messages: #region Scrollbar positions functions
/// <summary>
/// Sends a win32 message to get the scrollbars' position.
/// </summary>
/// <returns>a POINT structre containing horizontal
/// and vertical scrollbar position.</returns>
private unsafe Win32.POINT GetScrollPos()
{
Win32.POINT res = new Win32.POINT();
IntPtr ptr = new IntPtr(&res);
Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ptr);
return res;
}
/// <summary>
/// Sends a win32 message to set scrollbars position.
/// </summary>
/// <param name="point">a POINT
/// conatining H/Vscrollbar scrollpos.</param>
private unsafe void SetScrollPos(Win32.POINT point)
{
IntPtr ptr = new IntPtr(&point);
Win32.SendMessage(Handle, Win32.EM_SETSCROLLPOS, 0, ptr);
}
#endregion
OnKeyDown method and not call the base method, keystrokes of keys containing actual characters are still handled (they arrive using the WM_CHAR message). Due to that I decided to override the WndProc method and filter unwanted keystrokes at that level.
RichTextBox and understood what I have to build for my needs. Hence my resulting RTF is probably not optimal. It probably has language limitations that can be solved fairly easily by manipulating the RTF header. ToCloseToken DescriptorType.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 13 Jul 2005 Editor: Smitha Vijayan |
Copyright 2005 by Uri Guy Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |