Library code snippets

Create Dynamic Menu in VB

Page 1 of 2
  1. Dynamic Menu Form coding
  2. Dynamic Menu Class coding

Dynamic Menu Form coding

Option Explicit

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    lblLabel.BackColor = vbButtonFace
    lblLabel.ForeColor = vbWindowText
   
End Sub


Private Sub lblLabel_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    lblLabel.BackColor = vbHighlight
    lblLabel.ForeColor = vbHighlightText

End Sub

Private Sub lblLabel_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim oMenu As mcPopupMenu: Set oMenu = New mcPopupMenu
    Dim oSubmenu1 As mcPopupMenu: Set oSubmenu1 = New mcPopupMenu
    Dim oSubmenu2 As mcPopupMenu: Set oSubmenu2 = New mcPopupMenu
    Dim oSubmenu3 As mcPopupMenu: Set oSubmenu3 = New mcPopupMenu
    Dim x1 As Single, y1 As Single, Ret As Long
   
    ' Labels don't have a handle, set x,y manually
    x1 = (Me.Left + 45 + lblLabel.Left) / 15 ' 15 twips per pixel
    y1 = (Me.Top + 525 + lblLabel.Height) / 15
   
    ' Add a few of the main menu items
    oMenu.Add 1, "Item 1"
    oMenu.Add 2, "Item 2", True, , , imglstImages.ListImages(1).Picture
    oMenu.Add 3, "Item 3", , , mceGrayed
   
    oMenu.Add 4, "-" ' Seperator
   
    ' Build our submenus when needed
    oSubmenu1.Caption = "Submenu 1"
    oSubmenu1.Add 20, "Submenu 1 Item 1", , , mceGrayed, imglstImages.ListImages(2).Picture
    oSubmenu1.Add 21, "Submenu 1 Item 2", , , , imglstImages.ListImages(3).Picture
    oSubmenu1.Add 22, "Submenu 1 Item 3", , , , imglstImages.ListImages(4).Picture
   
    ' Submenu of the first submenu
    oSubmenu2.Caption = "Submenu 2"
    oSubmenu2.Add 30, "Submenu 2 Item 1"
    oSubmenu2.Add 31, "Submenu 2 Item 2"
    oSubmenu2.Add 32, "Submenu 2 Item 3"
   
    ' Add second submenu to first
    oSubmenu1.Add 33, oSubmenu2
   
    ' Add first submenu to main.
    oMenu.Add 5, oSubmenu1
   
    oMenu.Add 6, "-" ' Another seperator
   
    ' Build third submenu
    oSubmenu3.Caption = "Submenu 3"
    oSubmenu3.Add 40, "Submenu 2 Item 1", , , , imglstImages.ListImages(2).Picture
    oSubmenu3.Add 41, "Submenu 3 Item 2", , , , imglstImages.ListImages(3).Picture
    oSubmenu3.Add 42, "Submenu 3 Item 3", , , , imglstImages.ListImages(4).Picture
   
    oMenu.Add 7, oSubmenu3
   
    oMenu.Add 8, "-" ' Yet another
   
    ' The remaining items in the main menu
    oMenu.Add 9, "Item 4", , True
    oMenu.Add 10, "Item 5", , , , imglstImages.ListImages(4).Picture
    oMenu.Add 11, "Item 6", , True, mceGrayed
   
    ' Show popup
    Ret = oMenu.Show(Me.Hwnd, x1, y1)
   
    ' Release objects
    Set oSubmenu1 = Nothing: Set oSubmenu2 = Nothing: Set oSubmenu3 = Nothing: Set oMenu = Nothing
       
    MsgBox "You chose menu ID " & Ret
   
    Select Case Ret
   
        ' Lights, camera, action!
   
    End Select

End Sub

 

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Create Dynamic Menu in VB.

  2. 07 Jul 2006 at 10:14

    it could do with the sample program

     

  3. 20 Nov 2006 at 10:53

    HI hari,

    very good job you have done.

    may i know how to add short cut key's to that menu.

  4. 26 Jun 2007 at 02:55

    hi khari6579,

    i have a question for your control. how can you make the submenus into 2 or more columns like the behavior of the StartMenu in Windows XP if the items are too many & the users does not like the default scrolling items.

    thank you very much.

  5. 29 Jun 2007 at 08:14
    Dear John
     You can able to add submenu like the behaviour of the win XP. Its also applicable in the given coding..

    I explained (Build our subment, first submenu and  second sub menu like that i mentioned in the code itself) in the coding itself and also commended that line

    Hari K......






  6. 29 Jun 2007 at 08:18
    Dear Ravi,

     Thanking you!!! your comments...

     If each menu is doing a Desktop application then we need to worry about the shortcut key for each and every menu. If you ask that how to add all the menus in the Systray then you need to add one more class for doing that purpose. Here i just given a example/logics for how to create menu dynamically..

    Have a nice day

    Regards
    Hari K......










  7. 05 May 2008 at 03:55

    Hi Hari,

     

    I have copied your code into a VB project but when I click on the lblLabel I get this error message:-

    "User-defined type not defined" when it hits this line...  "Dim oMenu As mcPopupMenu"

    What am I doing wrong?

    Regards,  Doug.

  8. 05 Jun 2009 at 19:09

    How can i create 3d looking keygens

  9. 14 Aug 2009 at 01:44

    Hi Hari

    Please help me

    when I run program, an error message "User-defined type not defined" at line Dim oMenu As mcPopupMenu

    Thanks.

  10. 24 Nov 2009 at 07:10

    how to create code for bar code reader

  11. 24 Nov 2009 at 07:11

    can you post sample program for record system

Leave a comment

Sign in or Join us (it's free).

Hari K I am Hari, TL in a Pvt Firm. I have knowledge in Dot Net, WM, VB6, Visual Foxpro, Foxpro, SQL Server, Oracle, Access, DBase and Crystal Report.

Related discussion

Related podcasts

  • Visual Studio Talk Show (en français): Christian Beauclair

    Published 1 year ago, running time 0h51m

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss... de, il, christian, vb6

Want to stay in touch with what's going on? Follow us on twitter or Facebook!