Context menu Item Location

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
Using the code below, I'm displaying a context menu item when the user right clicks the mouse over a listview. The problem is that the menu item appears in the top left corner of the screen. Not only is it not over the listview, it's not even over the form itself. It sits alone on the desktop in the upper left corner.

Anyhelp?

Visual Basic:
    Private Sub lvwLeftList_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvwLeftList.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.cmnuStrip1.Show()
        End If
    End Sub

    Private Sub cmnuSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuSelectAll.Click
        Dim ListLeft As ListViewItem
        For Each ListLeft In lvwLeftList.Items
            ListLeft.Selected = True
        Next
    End Sub
 
Well I'm slightly confused by your code, assuming that cmnuStrip1 is a ContextMenu object of some kind. Since in my Visual Studio this only has one overload for the Show() method, and that requires a control and a position I can only assume you have a newer version or are using a different control (perhaps a .Net 2.0 control).

Once a context menu is added as a component to the form I would show it like so....

Visual Basic:
' assuming cmnuStrip1 is a context menu like so
Dim cmnuStrip1 as new ContextMenu()

' show the context menu at a point slightly offset from the control
cmnuStrip1.Show(lvwLeftList, new Point(10, 10))
 
Thanks. Can it be located near the current cursor location somhow?

Cags said:
Well I'm slightly confused by your code, assuming that cmnuStrip1 is a ContextMenu object of some kind. Since in my Visual Studio this only has one overload for the Show() method, and that requires a control and a position I can only assume you have a newer version or are using a different control (perhaps a .Net 2.0 control).

Once a context menu is added as a component to the form I would show it like so....

Visual Basic:
' assuming cmnuStrip1 is a context menu like so
Dim cmnuStrip1 as new ContextMenu()

' show the context menu at a point slightly offset from the control
cmnuStrip1.Show(lvwLeftList, new Point(10, 10))
 
This should do the trick, might not be the best way but it's the only way I could come up with off the top of my head.
Visual Basic:
Dim tmpPoint As Point = PointToClient(MousePosition)
cmnuStrip1.Show(lvwLeftList, New Point(tmpPoint.X - Button1.Left, tmpPoint.Y - Button1.Top))
 
A context menu strip (.Net 2.0) and a context menu (.Net 1.0+) are two different components (one uses Window's built in menus while the other uses a customized Office style menu). Though they perform the same function, you should be aware that there may be differences between their interfaces.
 
Back
Top