SelectedText Property

RichTextBox.SelectedText Property

Gets or sets the selected text within the RichTextBox.

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

public override string SelectedText { get; set; }
/** @property */
public String get_SelectedText ()

/** @property */
public void set_SelectedText (String value)

public override function get SelectedText () : String

public override function set SelectedText (value : String)

Not applicable.

Property Value

A string that represents the selected text in the control.

The following code example demonstrates how to use the SelectionBullet property with the SelectionFont, SelectedText, and SelectionColor properties to create a bulleted list in a RichTextBox control. This example requires that a RichTextBox control named richTextBox1 is created on a form.

private void WriteTextToRichTextBox()
{
   // Clear all text from the RichTextBox;
   richTextBox1.Clear();
   // Set the font for the opening text to a larger Arial font;
   richTextBox1.SelectionFont = new Font("Arial", 16);
   // Assign the introduction text to the RichTextBox control.
   richTextBox1.SelectedText = "The following is a list of bulleted items:" + "\n";
   // Set the Font for the first item to a smaller size Arial font.
   richTextBox1.SelectionFont = new Font("Arial", 12);
   // Specify that the following items are to be added to a bulleted list.
   richTextBox1.SelectionBullet = true;
   // Set the color of the item text.
   richTextBox1.SelectionColor = Color.Red;
   // Assign the text to the bulleted item.
   richTextBox1.SelectedText = "Apples" + "\n";
   // Apply same font since font settings do not carry to next line.
   richTextBox1.SelectionFont = new Font("Arial", 12);
   richTextBox1.SelectionColor = Color.Orange;
   richTextBox1.SelectedText = "Oranges" + "\n";
   richTextBox1.SelectionFont = new Font("Arial", 12);
   richTextBox1.SelectionColor = Color.Purple;
   richTextBox1.SelectedText = "Grapes" + "\n";
   // End the bulleted list.
   richTextBox1.SelectionBullet = false;
   // Specify the font size and string for text displayed below bulleted list.
   richTextBox1.SelectionFont = new Font("Arial", 16);
   richTextBox1.SelectedText = "Bulleted Text Complete!";
}

private void WriteTextToRichTextBox()
{
    // Clear all text from the RichTextBox;
    richTextBox1.Clear();
    // Set the font for the opening text to a larger Arial font;
    richTextBox1.set_SelectionFont(new Font("Arial", 16));
    // Assign the introduction text to the RichTextBox control.
    richTextBox1.set_SelectedText("The following is a list of bulleted items:"
        + "\n");
    // Set the Font for the first item to a smaller size Arial font.
    richTextBox1.set_SelectionFont(new Font("Arial", 12));
    // Specify that the following items are to be added to a bulleted list.
    richTextBox1.set_SelectionBullet(true);
    // Set the color of the item text.
    richTextBox1.set_SelectionColor(Color.get_Red());
    // Assign the text to the bulleted item.
    richTextBox1.set_SelectedText("Apples" + "\n");
    // Apply same font since font settings do not carry to next line.
    richTextBox1.set_SelectionFont(new Font("Arial", 12));
    richTextBox1.set_SelectionColor(Color.get_Orange());
    richTextBox1.set_SelectedText("Oranges" + "\n");
    richTextBox1.set_SelectionFont(new Font("Arial", 12));
    richTextBox1.set_SelectionColor(Color.get_Purple());
    richTextBox1.set_SelectedText("Grapes" + "\n");
    // End the bulleted list.
    richTextBox1.set_SelectionBullet(false);
    // Specify the font size and string for text displayed below bulleted list.
    richTextBox1.set_SelectionFont(new Font("Arial", 16));
    richTextBox1.set_SelectedText("Bulleted Text Complete!");
} //WriteTextToRichTextBox

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

Community Additions

ADD
Show:
© 2016 Microsoft