• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9682
  • Last Modified:

VB.NET 2005 DataGridView ContextStripMenu (Right Click to get a programatically created context menu)

I have a form with a DataGridView control on it and I am trying to add a right click context menu when the user right clicks on it.... I have been able to add the context menu but all of my items on the menu are launching when the user right clicks and then the context menu is displayed.... here is the current code:

    Private Sub dgAssignedItems_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgAssignedItems.CellMouseUp

        If e.Button = Windows.Forms.MouseButtons.Right Then

            Dim grid As DataGridView = sender
            Dim menu As ContextMenuStrip = New ContextMenuStrip
            menu.Items.Add("Task1", Nothing, Test1)
            menu.Items.Add("Task2", Nothing, Test1)
            Dim pt As Point = grid.PointToClient(Control.MousePosition)
            menu.Show(dgAssignedItems, pt.X, pt.Y)

        End If

    End Sub

    Public Function Test1()

        MsgBox("test1")

    End Function


So the behavior that I am getting right now is when I right click on the datagrid control I get 2 message boxes with "test1" in them and then I see the context menu....
0
dev-adam
Asked:
dev-adam
1 Solution
 
jpaulinoCommented:
Change it to this:

   Private Sub dgAssignedItems_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgAssignedItems.CellMouseUp

        If e.Button = Windows.Forms.MouseButtons.Right Then

            Dim grid As DataGridView = sender
            Dim menu As ContextMenuStrip = New ContextMenuStrip
            menu.Items.Add("Task1", Nothing, AddressOf Test1)
            menu.Items.Add("Task2", Nothing, AddressOf Test2)
            Dim pt As Point = grid.PointToClient(Control.MousePosition)
            menu.Show(dgAssignedItems, pt.X, pt.Y)

        End If

    End Sub

    Public Function Test1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox("test1")
    End Function


    Public Function Test2(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox("test2")
    End Function
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now