A newer version of this page is available. Switch to the current version.

TextEdit.SelectionLength Property

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

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v19.1.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.

Examples

The following code pastes text from the Clipboard to a TextEdit control. The text is pasted only if the Clipboard contains data in text format.

If the text editor contains selected text before pasting, a message box appears asking about overriding the selection. If the end-user presses the No button, the text from the Clipboard is inserted at the end of the selection. Otherwise, the text is pasted over the selection.

private void button1_Click(object sender, System.EventArgs e) {
    if (Clipboard.GetDataObject().GetDataPresent("System.String") == true) {
        if (textEdit1.SelectionLength > 0) {
            if (MessageBox.Show("Do you want to paste over current selection?",
              "Paste Example", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) ==
                DialogResult.No) {
                textEdit1.SelectionStart += textEdit1.SelectionLength;
                textEdit1.SelectionLength = 0;
            }
        }
    }
    textEdit1.Paste();
}
See Also