Home > Geek Tips > The RichTextBox control in C# has issues…

The RichTextBox control in C# has issues…

Since I had such a hard time with this control, I decided to post my findings in the hopes that I can spread the knowledge to other people having the same issues.

The task at hand was to replicate a normal multi-lined TextBox control, but be able to add some color to each line of text. In a normal TextBox, you can call the AppendText() method and the control will automatically scroll to the bottom for you. Why oh why can’t the RichTextBox be so easy?

In order to overcome this problem, I created a new class to inherit from RichTextBox. My code looks something like this:

public class MyRichTextBox: RichTextBox
{
	/// <remarks>
	/// This has to inherit from the RichTextBox so we can
	/// override some of the events to properly hide the
	/// caret in the desired way (to only show when selecting text)
	/// </remarks>
	public MyRichTextBox()
	{
		// Set the default property values
		this.ReadOnly = true;
		this.Multiline = true;
		this.BackColor = System.Drawing.Color.White;
		this.Font = new System.Drawing.Font( "Verdana", 10 );
		this.Dock = DockStyle.Fill;
	}
 
	/// <summary>
	/// Override the OnTextChanged event to hide the caret
	/// </summary>
	/// <param name="e"></param>
	protected override void OnTextChanged( EventArgs e )
	{
		base.OnTextChanged( e );
		HideCaret( this.Handle );
	}
 
	/// <summary>
	/// An override of this event to prevent the window from system beeping
	/// </summary>
	/// <param name="e"></param>
	protected override void OnKeyPress( KeyPressEventArgs e )
	{
		// Do not let anything else try to process this event
		e.Handled = true;
	}
 
	/// <summary>
	/// Override the OnClick event to hide the caret
	/// </summary>
	/// <param name="e"></param>
	protected override void OnClick( EventArgs e )
	{
		base.OnClick( e );
		HideCaret( this.Handle );
	}
 
	/// <summary>
	/// Override the OnGotFocus event to hide the caret
	/// </summary>
	/// <param name="e"></param>
	protected override void OnGotFocus( EventArgs e )
	{
		base.OnGotFocus(e);
		HideCaret( this.Handle );
	}
 
	/// <summary>
	/// Hides the original function that appends text and replaces
	/// it with this version
	/// </summary>
	/// <remarks>
	/// This makes the AppendText() function more like the
	/// AppendText() function of a TextBox, which scrolls
	/// to the bottom of the text after it appends the new text
	/// </remarks>
	/// <param name="message"></param>
	public void AppendText( string text, System.Drawing.Color color )
	{
		// The first text in the textbox can stay on the first line
		if ( this.Text.Length != 0 )
			base.AppendText( Environment.NewLine );
 
		// Add our text with the specified color
		this.SelectionColor = color;
		this.SelectedText = text;
 
		// Now do what we need in order to scroll to the bottom and hide the caret
		this.Select( this.Text.Length, 0 );
		this.ScrollToCaret();
		HideCaret( this.Handle );
	}
 
	/// <summary>
	/// A DLL import that will help us hide the caret in the RichTextBox
	/// </summary>
	/// <param name="hwnd"></param>
	/// <returns></returns>
	[DllImport( "user32.dll", EntryPoint = "HideCaret" )]
	private static extern bool HideCaret( IntPtr hwnd );
}

Categories: Geek Tips Tags:
  1. No comments yet.
  1. No trackbacks yet.

Spam protection by WP Captcha-Free