contextmenu datagridview

Richnl

Well-known member
Joined
Mar 20, 2007
Messages
93
Programming Experience
Beginner
Does anybody have a clue why this is not working anymore
because it did before
It works for every other control, but it stopped working for this one
VB.NET:
    Private Sub dgv_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv.MouseDown

        Dim popMNU = New ContextMenu
        Dim mi As MenuItem
        mi = New MenuItem("Sorteer UP")
        AddHandler mi.Click, AddressOf SortUpMNUHandler
        popMNU.MenuItems.Add(mi)
        mi = New MenuItem("Sorteer Down")
        AddHandler mi.Click, AddressOf SortDownMNUHandler
        popMNU.MenuItems.Add(mi)
             dgv.ContextMenu = popMNU
    End Sub

I changed it to make it work
Set it to the form control
and
Private Sub dgvJobs_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvJobs.MouseLeave
Me.ContextMenu = Nothing
End Sub

it's a little odd

thanks, Richard
 
Last edited:
Euww.. you dynamically build the same context menu each time you mouse down on the component? Why not just make ONE at design time, assign it to the component as design time, and then at runtime figure out which control caused it to appear using the SENDER property of the event. The way you have it right now.. *shudder* - every time the user mouses down, a new menu is made and handlers attached to it.. Not nice!
 
Yes, not very elegant, but I figured for this tiny bit off code -
am not botherd that much
but for better programming, I can move that code to another place

thanks for pointing that out
 
Back
Top