Back to Devexpress

TextEdit.Copy() Method

windowsforms-devexpress-dot-xtraeditors-dot-textedit-06ba5b29.md

latest5.4 KB
Original Source

TextEdit.Copy() Method

Copies the current selection to the Clipboard.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public void Copy()
vb
Public Sub Copy

Remarks

Use the Copy method to copy the selected text to the Clipboard. Note that end-users can copy the selection using the CTRL+C or CTRL+INS key combinations or using the editor’s built-in context menu. The Copy method is useful if you need to provide additional methods to copy 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.

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

See Also

Cut()

DeselectAll()

Paste()

Reset()

ScrollToCaret()

SelectAll()

SelectedText

SelectionLength

SelectionStart

TextEdit Class

TextEdit Members

DevExpress.XtraEditors Namespace