Rich Text Box Control

The RichTextBox control for Windows Forms is used for displaying, typing and formatting of text. The control RichTextBox does everything the control of the TextBox does but can show fonts, colors and links; load images and text from a file; and find the characters that we specify.

The RichTextBox control is typically used for manipulating texts and displaying functionality similar to text processing applications such as Microsoft Word. Like Textbox, scroll bars can be displayed with RichTextBox control ; unlike TextBox, its default configuration is to show scroll bars both horizontal and vertical as required and has extra scroll bar configurations.

Rich Text Box Conrtol C# GUI

The text displayed is set by the Text property, just like the TextBox command. The controller RichTextBox has many text formatting characteristics.

How to Set Font Attributes for RichTextBox Control?


richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);  
richTextBox1.SelectionColor = System.Drawing.Color.Red;  

How to Set Indents, Hanging Indents, and Bulleted Paragraphs with RichTextBox Control?

To format a paragraph as a bulleted list


richTextBox1.SelectionBullet = true;

To indent a paragraph


richTextBox1.SelectionIndent = 8;  
richTextBox1.SelectionHangingIndent = 3;  
richTextBox1.SelectionRightIndent = 12;

The LoadFile and SaveFile processes can show and write several file format including plain text, Unicode plain text and Rich Text Format (RTF) to manipulate documents.

The possible file formats are listed in RichTextBoxStreamType

Specifies the input and output stream types for loading and saving data in the RichTextBox control.

Fields   Description
Rich Text 0 A RTF (Rich Text Format) stream.
PlainText 1 A plain text stream containing space in OLE (objects Linking and Embedding) objects.
TextTextOleObjs 2 A plain text stream, representing OLE objects textually. This is valid only for use with the RichTextBox, SaveFile(String) method.
UnicodePlainText 3 A text stream that includes object spaces instead of objects linked and embedded (OLE). In Unicode, the text is encoded.
RichNoOleObjs 4 A Rich Text Format (RTF) stream with OLE objects spaces. This value is valid only for use with the RichTextBox control, SaveFile(String) method.

 

For links in web-type style, we can also use a RichTextBox control by setting DetectUrls to true and write code to handle the LinkClicked event.


private void Link_Clicked (object sender, System.Windows.Forms.LinkClickedEventArgs e)
{
   System.Diagnostics.Process.Start(e.LinkText);
}

By setting the SelectionProtected property to true, we can stop the user from manipulate some or all of the text in the control.



private void ProtectMySelectedText()
{
   // Determine if the selected text in the control contains the word " My RichTextBox".
   if(richTextBox1.SelectedText != " My RichTextBox")
   {
      // Search for the word RichTextBox in the control.
      if(richTextBox1.Find("My RichTextBox",RichTextBoxFinds.WholeWord)== -1)
      {
         //Alert the user that the word was not foun and return.
         MessageBox.Show("The text \" My RichTextBox\" was not found!");
         return;
      }
   }
   // Protect the selected text in the control from being altered.
   richTextBox1.SelectionProtected = true;
}

By using the Undo and Redo techniques, We can undo and rework most of the operations in a RichTextBox control. The CanRedo method can help us decide if the control can be reapplied to the last operation that the user has undone.


private void RedoAllButDeletes()
{
	// Determines if a Redo operation can be performed.
	if(richTextBox1.CanRedo == true)
	{
		// Determines if the redo operation deletes text.
		if (richTextBox1.RedoActionName != "Delete")
			// Perform the redo.
			richTextBox1.Redo();
	}
}

private void Menu_Undo(System.Object sender, System.EventArgs e)
 {
    // Determine if last operation can be undone in text box.   
    if(textBox1.CanUndo == true)
    {
       // Undo the last operation.
       textBox1.Undo();
       // Clear the undo buffer to prevent last action from being redone.
       textBox1.ClearUndo();
    }
 }

 

Complete Source Code


using System;
using System.Drawing;
using System.Windows.Forms;

namespace RichTextBoxControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            rtxtTextBox.SaveFile("My Rich Text File.rtf", RichTextBoxStreamType.RichText);
            MessageBox.Show("File Saved!");
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            rtxtTextBox.LoadFile("My Rich Text File.rtf");
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            rtxtTextBox.Find("Text Box", RichTextBoxFinds.MatchCase);
            rtxtTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
            rtxtTextBox.SelectionColor= Color.Red;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if(rtxtTextBox.SelectionLength>0)
            {
                rtxtTextBox.Copy();
            }
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            if(Clipboard.GetDataObject().GetDataPresent("Text")==true)
            {
                if(rtxtTextBox.SelectionLength>0)
                {
                    if(MessageBox.Show("Do you want to paste over current Selection","Paste Example",MessageBoxButtons.YesNo)==DialogResult.No)
                    {
                        rtxtTextBox.SelectionStart = rtxtTextBox.SelectionStart + rtxtTextBox.SelectionLength;
                    }
                    rtxtTextBox.Paste();
                }

            }
        }

        private void btnUndo_Click(object sender, EventArgs e)
        {
            if(rtxtTextBox.CanUndo==true)
            {
                rtxtTextBox.Undo();
               // rtxtTextBox.ClearUndo();
            }
        }

        private void btnRedo_Click(object sender, EventArgs e)
        {
            if(rtxtTextBox.CanRedo==true)
            {
                if (rtxtTextBox.RedoActionName != "Delete")
                    rtxtTextBox.Redo();
            }
        }

        private void btnCut_Click(object sender, EventArgs e)
        {
            if(rtxtTextBox.SelectedText!="")
            {
                rtxtTextBox.Cut();
            }
        }
    }
}

 

Rich Text Box Control