How to use the C# RichTextBox

The RichTextBox in C# is a text editing control that can be used to display and edit content in rich text format. Here are some common usage scenarios:

  1. The text in the richTextBox has been set to “Hello, World!”.
  2. Set the font and size:
    richTextBox.Font = new Font(“Arial”, 12); // Set the font to Arial and font size to 12.
  3. Set text style:
    richTextBox.SelectionFont = new Font(richTextBox.Font, FontStyle.Bold); // Make selected text bold
    richTextBox.SelectionColor = Color.Red; // Make selected text red
  4. Add image:
    Image image = Image.FromFile(“image.jpg”);
    Clipboard.SetImage(image);
    richTextBox.Paste(); // Paste the image from the clipboard into the RichTextBox
  5. Search and replace text:
    int index = richTextBox.Find(“World”); // Search for “World” in the text and return the index of the first match
    if (index >= 0)
    {
    richTextBox.Select(index, “World”.Length); // Select the matching text
    richTextBox.SelectedText = “Universe”; // Replace the selected text with “Universe”
    }
  6. Save and load text:
    richTextBox.SaveFile(“document.rtf”); // Save text to an RTF file
    richTextBox.LoadFile(“document.rtf”); // Load text from an RTF file

These are just some common uses of RichTextBox; you can implement more complex functionalities by using other methods and properties according to your actual needs.

bannerAds