Sunday 8 January 2012

Cut / Copy Selected Text from Richtextbox And Paste into RichtextBox


Private Sub btnCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCut.Click
        If RichTextBox1.SelectedText <> "" Then
            'Copy the information to the clipboard
            Clipboard.SetText(RichTextBox1.SelectedText)
'Replace SelectedText with Null Value
            RichTextBox1.SelectedText = ""
        Else
            'If no text was selected, print out an error message box
            MsgBox("No text is selected to Cut")
        End If
    End Sub

Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        'Checks to see if the user selected anything
        If RichTextBox1.SelectedText <> "" Then
            'Copy the information to the clipboard
            Clipboard.SetText(RichTextBox1.SelectedText)
        Else
            'If no text was selected, print out an error message box
            MsgBox("No text is selected to copy")
        End If
    End Sub

Private Sub btnPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaste.Click
        'Get the data stored in the clipboard
        Dim iData As IDataObject = Clipboard.GetDataObject()
        'Check to see if the data is in a text format
        If iData.GetDataPresent(DataFormats.Text) Then
            'If it's text, then paste it into the textbox
            RichTextBox1.SelectedText = CType(iData.GetData(DataFormats.Text), String)
        Else
            'If it's not text, print a warning message
            MsgBox("Data in the clipboard is not availble for entry into a textbox")
        End If
    End Sub


Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
        Clipboard.Clear()
        MsgBox("Data in the clipboard is Cleared")
    End Sub  



2 comments:

  1. Nice Coding, but i want to do the same in the form inside an MDIform.Could you please let me know your suggestion?

    Thank you
    M D Shivaraman

    ReplyDelete