A class for API Created Dynamic Menus


For some controls In my current project, I wanted To be able To display a simple context menu using the Right mouse button.

The solution In plain VB uses an existing menu, which must be extended (at runtime Or at design Time). I found this unacceptable.

Searching the web offered the solution of using the API functions CreatePopupMenu(), TrackPopupMenu() And AppendMenu().

I extended the sample To work With a undetermined number of arguments, thus giving Me the flexibility I needed.

The presented Function Popup() assumes the user wants a context menu at the current mouse pointer location. The argument Array consists of menu items Or separators (presented by a Single dash). The user can Select a menu Item by using either the Left Or Right mouse button. The index of the chosen menu Item Is returned, Or zero If cancelled.

A simple usage of the class module could be : (on a list box)


private Sub List1_MouseUp(Button as Integer, Shift as Integer, _
      x as Single, y as Single)
    Dim oMenu as cPopupMenu
    Dim lMenuChosen as Long
'
    If Button = vbRightButton then
        set oMenu = new cPopupMenu
'
' Pass in the desired menu, use '-' for a separator
'
        lMenuChosen = oMenu.Popup("Menu 1", "Menu 2", "Menu 3", _
                "-", "Menu 4")
'
        Debug.print lMenuChosen
    End If
'
End Sub
'
'

Download Zipped Project File and API Menu Class (3k)



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds