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

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?

Matti
0
Matti
Asked:
Matti
  • 3
1 Solution
 
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...
0
 
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.
 
Matti
0
 
MattiAuthor Commented:

'Declares:
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.


Matti

0
 
Bob LearnedCommented:
Matti,

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

Bob
0
 
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.

Matti
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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