Skip to main content

TextEdit.SelectionLength Property

Gets or sets the number of characters selected in the text box.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v24.2.dll

Declaration

[Browsable(false)]
public int SelectionLength { get; set; }

Property Value

Type Description
Int32

An integer value specifying the number of characters selected in the text box.

Remarks

The SelectionLength property specifies the number of characters selected within the editor. The first selected character is referred by the TextEdit.SelectionStart property. These properties can be used together to select text in the editor. When the value of the TextEdit.SelectionLength property is set to a value that is larger than the number of characters within the edit box, the value of the TextEdit.SelectionLength property is set to the entire length of text within the control minus the value of the TextEdit.SelectionStart property (if any value is specified for the TextEdit.SelectionStart property).

You can use SelectionLength to determine if any characters are currently selected in the text editor before performing operations on the selected text.

Example

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();
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the SelectionLength property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also