Solved

Dynamic menus. Create menus at runtime...

Posted on 2002-05-02
9
315 Views
Last Modified: 2010-05-02
I am trying to create dynamic menus at runtime i.e. I want to generate menu items at runtime  by  picking the menu items from a database, but I am unable to find the options to implement that. I have tried using menuitems and giving the menuitems an index value, thus creating a control array for it - but with this the problem is that I cannot create submenuitems for the dropdown menus I am creating. Also I am trying to generate the top level menu items dynamically which with this approach is giving a problem. I have also tried using commandbar control but that always gives wrong number of arguments error or runtime error 5. I would be glad if anyone of you experts could help me out with this problem or could suggest an alternate solution.
0
Comment
Question by:joyriderank
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6985655
If you use the standard menu editor to create a control array of menu items with one item having index 0 in each menu could be shown then you can use the load command to instantiate subsequent menu items.
0
 
LVL 1

Author Comment

by:joyriderank
ID: 6985754
TimeCottee,

Thanks for the prompt response. I have tried that but I have to create a multi level menu and all the heads have to come dynamically. This much I have been able to acomplish but I need to get more functionality added to the menu on runtime.
0
 
LVL 13

Expert Comment

by:crazyman
ID: 6985764
VbAccelerator has many excellent menu solutions.

http://www.vbaccelerator.com/overctl.htm
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6985831
Thinking of alternatives, I use a TreeView control to populate a menu hierarchy from a database. It provides an excellent interface and is extremely flexible.
0
IT, Stop Being Called Into Every Meeting

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!

 
LVL 18

Expert Comment

by:mdougan
ID: 6985982
This example will show you how to dynamically add some menu items to the System menu of a form.  Simply pass the hwnd of the form to the SubClass function.  Keep in mind that it's one thing to add the menu items, but then it's another to be able to respond when the user clicks those menu items.  So, it's necessary to "hook" the "window proc" of the window with the menu.  This will force all window commands through your custom window proc (in the example below that is the SysMenuHandler routine) which will check to see if the message was generated by clicking on one of your dynamic menus, and if so, it will execute some code.  In all cases, it then sends the message along to the original window proc.

There are also APIs for appending sub-menus, and for getting handles to other menus besides the SystemMenu, but I'm not that familiar with that.  But, this should get you looking in the right direction.

Option Explicit
Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long
Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Const WM_SYSCOMMAND = &H112
Public Const MF_SEPARATOR = &H800&
Public Const MF_STRING = &H0&
Public Const GWL_WNDPROC = (-4)
Public Const IDM_CUSTOM As Long = 1010
Public Const IDM_CUSTOM2 As Long = 1011
Public lProcOld As Long
Public ghWnd As Long

Public Function SysMenuHandler(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If iMsg = WM_SYSCOMMAND Then
        If wParam = IDM_CUSTOM Then
            MsgBox "About...", vbInformation, "About"
            Exit Function
        End If
        If wParam = IDM_CUSTOM2 Then
            MsgBox "Unhooking...", vbInformation, "Unhook"
            SetWindowLong ghWnd, GWL_WNDPROC, lProcOld
            Exit Function
        End If
    End If
    SysMenuHandler = CallWindowProc(lProcOld, hwnd, iMsg, wParam, lParam)
End Function

Public Function SubClass(hwnd As Long)
    Dim lhSysMenu As Long, lRet As Long
    lhSysMenu = GetSystemMenu(hwnd, 0&)
    'Add seperator
    lRet = AppendMenu(lhSysMenu, MF_SEPARATOR, 0&, vbNullString)
    'Add new menu item
    lRet = AppendMenu(lhSysMenu, MF_STRING, IDM_CUSTOM, "About...")
    lRet = AppendMenu(lhSysMenu, MF_STRING, IDM_CUSTOM2, "Unhook")
    lProcOld = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SysMenuHandler)
End Function
0
 
LVL 12

Accepted Solution

by:
jgv earned 200 total points
ID: 6986772
This sample should provide what you are looking for.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=5566&lngWId=1
0
 
LVL 12

Expert Comment

by:jgv
ID: 6986775
This sample should provide what you are looking for.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=5566&lngWId=1
0
 
LVL 1

Author Comment

by:joyriderank
ID: 6989476
Thanks for all your comments and suggestion. I have been kept a little buy lately. I will go through your suggestions in the weekend and get back to you guys on Monday. Thanks for your help guys. TAKE CARE.

Regards,
Joyriderank.
0
 
LVL 1

Author Comment

by:joyriderank
ID: 7020771
Hi Experts,

Sorry for the delay in getting back to you. First I would like to thank for your suggestions.

MDOUGAN thanks for the code but I am sorry to say I could not make out much from it.

IGV your code was really helpful as it got me to getting the menus created via database connectivity through modifications of course, but I am not able to trap the events for each click. What I mean to say is that now that i have the structure of the menus ready, but what good are they if I can't trap the events of the menus and call the respective form.... So I guess I will raise that question in a new question and am going to post another query regarding the same pretty soon.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 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

22 Experts available now in Live!

Get 1:1 Help Now