Add ContextMenu with Cut,Copy and paste options to Textbox in


In this article we learn that how to add Context menu to Textbox with Cut, Copy and paste options on runtime, this article also show that how to add context menu to any control at runtime that has these options:

Cut : Moves the current selection in the text box to the Clipboard,This method will only cut text from the text box if text is selected in the control. You can use this method, instead of using the Clipboard class, to copy text in the text box and move it to the Clipboard.

Copy: copy the selected text to clipboard.

paste: paste the text that has clipboard.

following example requires one windows form with mulitline textbox control named ‘TextBox1’.


<span style="color: #0000ff;">Private Sub</span> Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
    Private Sub AddContextMenu()
        Dim Contextmenu1 As New ContextMenu
        Dim menuItem1Cut As New MenuItem("Cut")
        AddHandler menuItem1Cut.Click, AddressOf menuItem1Cut_Click
        Dim menuItem2Copy As New MenuItem("Copy")
        AddHandler menuItem2Copy.Click, AddressOf menuItem2Copy_Click
        Dim menuItem3Paste As New MenuItem("Paste")
        AddHandler menuItem3Paste.Click, AddressOf menuItem3Paste_Click
        TextBox1.ContextMenu = Contextmenu1
    End Sub
Private Sub menuItem1Cut_Click()
    End Sub
    Private Sub menuItem2Copy_Click()
    End Sub
    Private Sub menuItem3Paste_Click()
    End Sub

we can use this code with RichtextBox control also with some modification.

2 thoughts on “Add ContextMenu with Cut,Copy and paste options to Textbox in”

  1. well every thing is good my problem is that when i have to paste a string in textbox i use

    Dim a As String
    a=”I am baid”

    but when i do the same with richtexttextbox
    Dim a As String
    a=”I am baid”

    It shows an error….

    i want to know the reason and method how can i do that

Comments are closed.