How do you place a menu

I'm using VB 2005 pro

I have a second form where I have made menu's. I wan't to call a menu from my frist form's button1_click event.
Menu open's ok but on upper left corner of the screen, how do i place it on the button, or there where cursor is?

LVL 14
Who is Participating?
Bob LearnedConnect With a Mentor Commented:

To show a ContextMenuStrip below a button when you left-click on a form, you could something like this:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
   If e.Button = Windows.Forms.MouseButtons.Left Then
    Me.ContextMenuStrip1.Show(Me.Button1, New Point(0, Me.Button1.Height))
   End If
End Sub

Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you talking about a ContextMenuStrip?

For the button in question, set its ContextMenuStrip property to the ContextMenuStrip you want to appear when it is right clicked...
MattiAuthor Commented:
ContextMenuStrip works on right click. I wan't  to show menu on button_click event (left click).

I have many options, so one if i could simulate right button click on button_click event it would do also.

How i have a form where i collect menus and i wan't to call a menu from there, but the menu appears on left corner of the screen, can i move it to the cursor before it's shown?

If i go for ContextMenuStrip there would be other places to code, as it appears every time i right click on form.
MattiAuthor Commented:

Public Structure POINTAPI
        Dim X As Integer
        Dim Y As Integer
    End Structure

Public Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Integer
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
        ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
        ByVal dwExtraInfo As Integer)

'Button_click to show the menu

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
          Dim mypos As POINTAPI, c As Integer
        Me.ContextMenuStrip = ContextMenuStrip1
        c = GetCursorPos(mypos)
        RightMouseClick(mypos.X, mypos.Y)
    End Sub

'Sub to simulate mouse click
    Private Sub RightMouseClick(ByVal x As Integer, ByVal y As Integer)
        mouse_event(&H8, x, y, 0, 0)
        mouse_event(&H10, x, y, 0, 0)
    End Sub

'code to prevent showing it when user right clicks on form, this code goes to several places
Private Sub Form1A_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Me.ContextMenuStrip = Nothing
    End Sub

I got this made, put the first click seems to go on uncontrol, my firewall gets that click.

I'm leaving it here overnight, i be back tomorrow.


MattiAuthor Commented:
Thaks, it's a simple .Net solution.

That night i did also solve my right click simulation problem, but i will not use it in this case.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.