Back to Devexpress

TextEdit.SelectionStart Property

windowsforms-devexpress-dot-xtraeditors-dot-textedit-da2eeff0.md

latest10.4 KB
Original Source

TextEdit.SelectionStart Property

Gets or sets the starting point of text selected in the text box.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[Browsable(false)]
public int SelectionStart { get; set; }
vb
<Browsable(False)>
Public Property SelectionStart As Integer

Property Value

TypeDescription
Int32

An integer value specifying the starting point of text selected in the text box.

|

Remarks

If no text is selected within the editor, this property indicates the insertion point for new text (the caret position). If you set this property to a location beyond the length of the text in the control, selection start position will be placed after the last character. If the remaining text in the control after the position indicated by the SelectionStart property is less than the value of the TextEdit.SelectionLength property, the value of the TextEdit.SelectionLength property is automatically decreased. The value of the SelectionStart property never causes an increase in the TextEdit.SelectionLength property.

You can programmatically move the caret within the text box by setting SelectionStart to the position within the text box where you want the caret to move to and set the TextEdit.SelectionLength property to zero.

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.

csharp
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();
}
vb
Imports DevExpress.XtraEditors

Private Sub pasteBarButtonItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) _
    Handles pasteBarButtonItem.ItemClick
    ' Check if there is any text in the Clipboard to paste into the text editor.
    If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) Then
        ' Check if any text is selected in the text editor.
        If textEdit1.SelectionLength > 0 Then
            ' 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 Then
                ' Move selection to the point after the current selection and paste.
                textEdit1.SelectionStart = textEdit1.SelectionStart + textEdit1.SelectionLength
            End If
        End If
        ' Paste the text into the text editor.
        textEdit1.Paste()
    End If
End Sub

Private Sub cutBarButtonItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) _
    Handles cutBarButtonItem.ItemClick
    ' Check if any text is selected in the text editor.   
    If textEdit1.SelectedText.Length > 0 Then
        ' Cut the selected text into the Clipboard.
        textEdit1.Cut()
    End If
End Sub

Private Sub copyBarButtonItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) _
    Handles copyBarButtonItem.ItemClick
    ' Check if any text is selected in the text editor.   
    If textEdit1.SelectionLength > 0 Then
        ' Copy the selected text into the Clipboard.
        textEdit1.Copy()
    End If
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the SelectionStart 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.

winforms-mvvm-best-practices/CS/Messenger/SendingAndReceivingMessagesUserControl.cs#L60

csharp
memo.Text += ("String message: " + message + Environment.NewLine);
memo.SelectionStart = memo.Text.Length;
memo.ScrollToCaret();

how-to-implement-a-custom-inplace-editor-for-appointments-e4826/CS/SchedulerInplaceEditorExample/MyInplaceEditor.cs#L82

csharp
edtSubject.SelectionLength = 0;
    edtSubject.SelectionStart = edtSubject.Text.Length;
}

winforms-dashboard-custom-items-extension/CS/CustomItemExtension/CustomItems/WebPageItem/UriEditForm.cs#L13

csharp
const string placeholder = "{0}";
int selectionStart = teUriPattern.SelectionStart;
int selectionlength = teUriPattern.SelectionLength;

winforms-grid-add-new-row-by-typing-in-new-item-row/CS/WindowsApplication3/Main.cs#L42

csharp
if(edit == null) return;
edit.SelectionStart = 1;
edit.SelectionLength = 0;

winforms-mvvm-best-practices/VB/Messenger/SendingAndReceivingMessagesUserControl.vb#L58

vb
memo.Text += ("String message: " & message & Environment.NewLine)
memo.SelectionStart = memo.Text.Length
memo.ScrollToCaret()

how-to-implement-a-custom-inplace-editor-for-appointments-e4826/VB/SchedulerInplaceEditorExample/MyInplaceEditor.vb#L80

vb
edtSubject.SelectionLength = 0
    edtSubject.SelectionStart = edtSubject.Text.Length
End If

winforms-dashboard-custom-items-extension/VB/CustomItemExtension/CustomItems/WebPageItem/UriEditForm.vb#L21

vb
Const placeholder As String = "{0}"
Dim selectionStart As Integer = teUriPattern.SelectionStart
Dim selectionlength As Integer = teUriPattern.SelectionLength

winforms-grid-add-new-row-by-typing-in-new-item-row/VB/WindowsApplication3/Main.vb#L36

vb
If edit Is Nothing Then Return
edit.SelectionStart = 1
edit.SelectionLength = 0

See Also

Copy()

Cut()

DeselectAll()

Paste()

Reset()

ScrollToCaret()

SelectAll()

SelectedText

SelectionLength

TextEdit Class

TextEdit Members

DevExpress.XtraEditors Namespace