Let us look at a simple C# Windows Forms RichTextBox example.
Example 1: RichTextBox
This example will comprise the following files:
RichTextBox.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). RichTextBox.cs
Create a file named RichTextBox.cs
Here is the full code
using System;
using System.Windows.Forms;
public static class RichTextBoxExtensions {
public static void AppendText(this System.Windows.Forms.RichTextBox box, string text, System.Drawing.Color color) {
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
}
namespace Examples {
class MainForm : Form {
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
public MainForm() {
Text = "RichTextBox example";
richTextBox.Parent = this;
richTextBox.Dock = DockStyle.Fill;
richTextBox.AppendText("Colored text :n");
richTextBox.SelectionStart = richTextBox.TextLength;
richTextBox.SelectionColor = System.Drawing.Color.Red;
richTextBox.AppendText(" Redn");
richTextBox.SelectionColor = System.Drawing.Color.Green;
richTextBox.AppendText(" Greenn");
richTextBox.SelectionColor = System.Drawing.Color.Blue;
richTextBox.AppendText(" Bluen");
richTextBox.SelectionColor = richTextBox.ForeColor;
}
private RichTextBox richTextBox = new RichTextBox();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |