Hello all,
In lots of applications there are copy,cut,and paste buttons where the user can highlight some text and click "Copy" or "Cut" or "Paste" and then the program would do the corresponding actions.
I was just wondering how to code such a button in Visual Basic 2010?

Thanks in advance

Member Avatar for Unhnd_Exception

clipboard.setText
clipboard.getText

Private Sub ToggleMenus()
        Try

            'Declare a TextBox object and set it to the ActiveControl
            Dim objTextBox As RichTextBox = Me.ActiveControl
            'Toggle the Undo menu items
            tsmUndo.Enabled = objTextBox.CanUndo

            'Toggle the Cut toolbar button and menu items
            tsmCut.Enabled = objTextBox.SelectionLength

            'Toggle the Copy toolbar button and menu items
            tsmCopy.Enabled = objTextBox.SelectionLength

            'Toggle the Paste toolbar button and menu items
            tsmpaste.Enabled = My.Computer.Clipboard.ContainsText

            'Toggle the Select All menu items
            tsmSelectAll.Enabled = _
            objTextBox.SelectionLength < objTextBox.Text.Length
End Sub 

 Private Sub tsmCut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsmCut.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
        Dim objTextBox As RichTextBox = Me.ActiveControl
        'Copy the text to the clipboard and clear the field
        objTextBox.Cut()
    End Sub

    Private Sub tsmCopy_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsmCopy.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
        Dim objTextBox As RichTextBox = Me.ActiveControl
        'Copy the text to the clipboard and clear the field
        objTextBox.Copy()
    End Sub

    Private Sub tsmPaste_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsmpaste.Click
        'Use My.Computer.Clipboard.GetText() or My.Computer.Clipboard.GetData to retrieve information from the clipboard.
        Dim objTextBox As RichTextBox = Me.ActiveControl
        'Copy the text to the clipboard and clear the field
        objTextBox.Paste()
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.