Here you are. Put a RichTextBox on a form and paste the code below to test. You can use this piece of code as my contribution in the codebook also. Let me know if you need any enhancements.
VB Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim rtbContextMenu As New ContextMenu() Dim mymenuitem As MenuItem mymenuitem = rtbContextMenu.MenuItems.Add("Copy") AddHandler mymenuitem.Click, AddressOf myCopy_Click mymenuitem = rtbContextMenu.MenuItems.Add("Paste") AddHandler mymenuitem.Click, AddressOf myPaste_Click RichTextBox1.ContextMenu = rtbContextMenu End Sub Private Sub myCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Clipboard.SetDataObject(RichTextBox1.SelectedText) End Sub Private Sub myPaste_Click(ByVal sender As Object, ByVal e As System.EventArgs) ' Declares an IDataObject to hold the data returned from the clipboard. ' Retrieves the data from the clipboard. Dim iData As IDataObject = Clipboard.GetDataObject() ' Determines whether the data is in a format you can use. If iData.GetDataPresent(DataFormats.Text) Then ' Yes it is, so display it in a text box. RichTextBox1.SelectedText = CType(iData.GetData(DataFormats.Text), String) Else ' No it is not. RichTextBox1.SelectedText = "Could not retrieve data off the clipboard." End If End Sub




Reply With Quote