Solved

context menu problem in vb.net?

Posted on 2013-01-03
2
655 Views
Last Modified: 2013-01-03
Hi everyone,

I wrote the below code for context menu in vb.net.

Private Sub InitializeMyContextMenu()
        ' Create the contextMenu and the MenuItem to add.
        Dim contextMenu1 As New ContextMenu()
        Dim mnuItemCut As New MenuItem("Cut")
        Dim mnuItemCopy As New MenuItem("&Copy")
        Dim mnuItemPaste As New MenuItem("&Paste")

        ' Use the MenuItems property to call the Add method
        ' to add the MenuItem to the MainMenu menu item collection. 
        contextMenu1.MenuItems.Add(mnuItemCut)


        contextMenu1.MenuItems.Add(mnuItemCopy)
        contextMenu1.MenuItems.Add(mnuItemPaste)

        ' Assign mainMenu1 to the rich text box.
       richtxtbox1.ContextMenu = contextMenu1
    End Sub

Open in new window

and the below code for copy & paste.

  Private Sub mnuItemCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemCopy.Click
        txtSRFormD_Instructions.Copy()

    End Sub
    Private Sub mnuItemCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemCut.Click
        txtSRFormD_Instructions.Cut()
      
    End Sub

Open in new window

I got the context menu though but the copy ans cut does not work?

Any bright ideas?

Thank you.
0
Comment
Question by:developer2012
2 Comments
 

Accepted Solution

by:
developer2012 earned 0 total points
ID: 38741437
Adding event handler a the time of menu item declaration resolved the issue.

 contextMenu1.MenuItems.Add(mnuItemCut)
        AddHandler mnuItemCut.Click, AddressOf Me.mnuItemCut_Click

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38741449
*Why do you have "Handles mnuItemCopy.Click" on the end of your methods if you are creating them at runtime and using AddHandler?...that would require you to either have "mnuItemCopy" already on the form, or you declared it at the form level using "WithEvents".
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now