How do you place a menu

Posted on 2006-05-05
Last Modified: 2012-06-27
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?

Question by:Matti
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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...
    LVL 14

    Author Comment

    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.
    LVL 14

    Author Comment


    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.


    LVL 96

    Accepted Solution


    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

    LVL 14

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now