This documentation is archived and is not being maintained.

RichTextBox.RichTextBox Constructor

Initializes a new instance of the RichTextBox class.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public RichTextBox ()
public RichTextBox ()
public function RichTextBox ()
Not applicable.

By default, the Multiline property of the control is set to true.

The following code example creates a RichTextBox control that loads an RTF file into the control and searches for the first instance of the word "Text." The code then changes the font style, font size, and font color of the selected text and saves the changes back to the original file. The example code finishes by adding the control to its Form. This example requires that the method created in the example code is added to a Form class and called from the constructor of the form. The example also requires that an RTF file is created, in the root of the C drive, containing the word "Text."

public void CreateMyRichTextBox()
{
    RichTextBox richTextBox1 = new RichTextBox();
    richTextBox1.Dock = DockStyle.Fill;


    richTextBox1.LoadFile("C:\\MyDocument.rtf");
    richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);

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

    richTextBox1.SaveFile("C:\\MyDocument.rtf", RichTextBoxStreamType.RichText);

    this.Controls.Add(richTextBox1);
}


public void CreateMyRichTextBox()
{
    RichTextBox richTextBox1 = new RichTextBox();

    richTextBox1.set_Dock(DockStyle.Fill);
    richTextBox1.LoadFile("C:\\MyDocument.rtf");
    richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);
    richTextBox1.set_SelectionFont(new Font
        ("Verdana", 12, FontStyle.Bold));
    richTextBox1.set_SelectionColor(Color.get_Red());
    richTextBox1.SaveFile("C:\\MyDocument.rtf",
        RichTextBoxStreamType.RichText);
    this.get_Controls().Add(richTextBox1);
} //CreateMyRichTextBox

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Show: