Using Rich TextBox In Windows Forms

INTRODUCTION

In this article, I am going to explain how to use a Rich TextBox in a Windows.Forms app using Visual Studio 2017.

STEP 1 - Create a new project

Let's create a new project using Visual Studio 2017.

Select New Project->Visual C#->Windows Forms App (.NET Framework), give your project a name and click OK.

Using Rich TextBox In Windows Forms 

This action creates a WinForms project with a default form and you should see the Windows Designer. 

STEP 2 - Drag and Drop Controls

Let's add a Rich TextBox control to the form by dragging it from the Toolbox and dropping it to the form. You will see that a Rich TextBox1 is added to the form. This control is now available for you in the code behind.

Using Rich TextBox In Windows Forms 

Rich TextBox Control

Rich TextBox has many formatting options. It applies the color, background colors, multiple fonts, and margins to the text. It adds more complexity than the regular Textbox. 

SelectionBackColor 

The SelectionBackColor property changes the currently selected text in the Rich TextBox to the background color you assign to it. If there is no current selection, then the caret is used to start the selection.

DetectUrls

When entering text in the Rich TextBox, sometimes a link to a Web Page will be entered. If we set the DetectUrls property to true, the Rich TextBox will add hyperlink styles to any possible link.

Using Rich TextBox In Windows Forms

Now, let's add other controls, like Panel, Color Dialog, Textbox, Button control to the form by dragging these from the Toolbox. You may also want to change the properties of the other controls.

Using Rich TextBox In Windows Forms

Change the text of other controls to what you like. 

STEP 3 - Coding for Button Click Event

You can add a button-click event handler by simply double-clicking on the button control event handler. We can display the text in Rich TextBox.
  1.   public partial class Form1 : Form    
  2.      {    
  3.          public Form1()    
  4.          {    
  5.              InitializeComponent();    
  6.          }    
  7.       
  8.          Color _color = Color.Blue;    
  9.          bool _first=true;    
  10.         private void button1_Click(object sender, EventArgs e)    
  11.         {    
  12.             richTextBox1.SelectionColor = _color;    
  13.             if (_first)    
  14.                 richTextBox1.SelectedText = textBox1.Text;    
  15.             else    
  16.                 richTextBox1.SelectedText = Environment.NewLine + textBox1.Text;    
  17.             _first = false;    
  18.             textBox1.Clear();    
  19.             textBox1.Focus();    
  20.         }    
  21.      
  22.         private void panel1_Paint(object sender, PaintEventArgs e)    
  23.         {    
  24.      
  25.         }    
  26.      
  27.         private void panel1_Click(object sender, EventArgs e)    
  28.         {    
  29.             if(colorDialog1.ShowDialog()== DialogResult.OK)    
  30.             {    
  31.                 _color = colorDialog1.Color;    
  32.                 panel1.BackColor = _color;    
  33.            }    
  34.         }    
  35.     }    
STEP 4 - Compile and Run

Now, simply compile and run the application. Enter the text in the Textbox. Click the panel control, a color selection window will be opened. Choose the color and click OK. After choosing the color, click the Send button. The text entered in the TextBox along with the color chosen will appear in the Rich TextBox.

Using Rich TextBox In Windows Forms

Using Rich TextBox In Windows Forms

Using Rich TextBox In Windows Forms 

Summary

In this basic article, you saw how to use a Rich TextBox control. Hope you found this article interesting. For any feedback, please post your comment at the bottom of this article. Thank you!.


Similar Articles