TextEdit.SelectedText Property

Gets or sets a value that indicates the selected text in the editor.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v20.1.dll

Declaration

[Browsable(false)]
public string SelectedText { get; set; }
<Browsable(False)>
Public Property SelectedText As String

Property Value

Type Description
String

A string value representing the selected text.

Remarks

Read the SelectedText property value to obtain the currently selected text. If no text is presently selected , the property returns an empty string. Assign a string to the SelectedText property to replace the currently selected text with the string specified. If no text is selected presently, assigning a value will insert the specified text to the current caret position. Note that changing the SelectedText property value removes the selection.

To select text via code, use the TextEdit.SelectionStart and TextEdit.SelectionLength properties.

Examples

The code below uses the TextEdit control. It provides ItemClick event handlers for BarItem objects that perform the Cut(), Copy(), and Paste() operations. This example requires that a TextEdit object named textEdit1 has been created.

using DevExpress.XtraEditors;

private void pasteBarButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
    // Check if there is any text in the Clipboard to paste into the text editor.
    if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)) {
        // Check if any text is selected in the text editor.
        if (textEdit1.SelectionLength > 0) {
            // Ask the user if they want to paste over the selected text.
            if (XtraMessageBox.Show("Do you want to paste over current selection?", "Paste", MessageBoxButtons.YesNo) == DialogResult.No)
                // Move selection to the point after the current selection and paste.
                textEdit1.SelectionStart = textEdit1.SelectionStart + textEdit1.SelectionLength;
        }
        // Paste the text into the text editor.
        textEdit1.Paste();
    }
}

private void cutBarButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
    // Check if any text is selected in the text editor.   
    if (textEdit1.SelectedText.Length > 0)
        // Cut the selected text into the Clipboard.
        textEdit1.Cut();
}

private void copyBarButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
    // Check if any text is selected in the text editor.   
    if (textEdit1.SelectionLength > 0)
        // Copy the selected text into the Clipboard.
        textEdit1.Copy();
}
See Also