Click to See Complete Forum and Search --> : Adding Submenus Dynamically
Eiredrake
Mar 1st, 2002, 03:14 PM
Much like the attachments, I need to add submenus dynamically based off of the results of a database query.
For each Project, there are a number of tasks defined. Each one needs a node. For each Task, there are a number of subtasks defined. And again each one needs it's own node.
Esentially, this is like a treeview, but it's with menus. This will actually be popping up out of the system tray.
Can anyone point me in the right direction? I've come across a large number of tutorials, but none of them handling submenus and sub-submenus.
Even a control would be helpful at this point.
Thanks
Eiredrake
VictorB212
Mar 2nd, 2002, 05:22 PM
I've figured out how to do exactly what you are asking for, and was even thinking about putting it in a class. Do you need to dynamically add them while your program is running, or does it only need to be set on startup?
VictorB212
Mar 2nd, 2002, 06:46 PM
Here's a little tray launcher program that I wrote. I have it dynamically creating menus from an .ini file, but you can easily change that. (I don't use the registry--when people delete my programs, they're gone.) You actually prompted me to fix a little bug, which I just did. Here's the project. Be forewarned that there are a few utility functions that the program does not call, as well as a bunch of unused declares and constants (in case they will be used later on). Ask me if you have any questions.
Be aware that you have to be very careful when your form is sublassed (you get a chance to look and act on the messages it receives (that a button was pressed, a mouse was moved, etc.)), for sometimes the IDE acts funny and it is much easier to crash your application or even computer. SAVE YOUR WORK OFTEN!!!
Eiredrake
Mar 4th, 2002, 08:56 AM
I would need to be able to add them on demand as needed. Most of them would be added at startup though.
Eiredrake
VictorB212
Mar 4th, 2002, 10:55 PM
Check out the mnuRefresh code. It deletes the dynamically created menus up to date and recreates them. If you need me to, I can probably write up a class...
What information is unique to each item in your menus? Will that be stored in an array of an intrinsic type, UDT, or what?
FYI, you cannot get a menu item that has subitems to respond to clicks itself.
I will only be able to offer significant help AFTER next Monday. Currently I am head of sound for my school play, with wireless mics...
VictorB212
Mar 7th, 2002, 10:47 PM
Have you figured this out or do you need me to make a class within the next week for dynamic menu creation?
Eiredrake
Mar 14th, 2002, 02:24 PM
Actually I need to create them dynamically from a tiered SQL query. An INI file I don't think would help much. I'm currently working with the API's listed for menus in Applemen's guide, but I'm not having much luck. I can't seem to make menu's appear.
Eiredrake
VictorB212
Mar 14th, 2002, 02:38 PM
I'm not sure I understand--you don't have to use an INI file. You just have to adapt my code to be used with a tiered data structure (i.e. main menus and submenus).
My code deals with how windows handles menus -- menu items get a handle that is passed to WindowProc unless they branch out to submenus. That is why I delete the elements of the array that lead to more submenus.
If you can parse your sql return into a tiered datastructure arrMenu() that I have created, then using the code I posted shouldn't be too hard. Give me the sql return, tell me how it should parse out, and then I'll tell you how to adapt the menu code.
Eiredrake
Mar 18th, 2002, 08:20 AM
I think I misunderstood your original posting. I've been reading through your code and I think it will allow me to solve my problem.
Thanks,
Eiredrake
Eiredrake
Mar 21st, 2002, 01:16 PM
Welp... i've created a sub to populate the items into the tray and it works alright.
Only problem is a need to get rid of them so I can repopulate on demand.
Any ideas before I go insane? Let me know if you want to see the code.
Eiredrake
VictorB212
Mar 21st, 2002, 02:31 PM
Check out the Subroutine included in that zip file I posted -- it's in basMenu and is called DeleteMenus().
Eiredrake
Mar 22nd, 2002, 08:27 AM
I think the problem is that I'm not storing anything to reference the menus with. I notice you're sticking that sort of thing into an array.
However I noticed that the createmenu sub is not always returning a handle value. Many times it's returning a 0. Which makes it difficult to later get the deletions correct.
any advice?
Eiredrake
VictorB212
Mar 22nd, 2002, 03:04 PM
Alright, I need to know your menu setup. It gets hairy when you have some submenus that are dynamically created and some static ones.
Menu 1 Menu 2
|-Submenu |-Submenu
| |-SubSub
|-Submenu
Something like that to just let me know how you've set things up. Specify which menus are always there (static) as opposed to dynamic. Bold/italic would be a good way to do it.
Give me feedback on this: I suggest that you wipe and totally rebuild your menus whenever you want to make a change. This way code will be a lot less complicated. Otherwise you have to worry about adding this menu there, etc. Rebuilding them is pretty simple, and the user will not even notice.
VictorB212
Mar 27th, 2002, 11:33 AM
Alright, it looks like I'll be making a menu class that encapsulates everything that one could want with dynamic menus. Here would be an example of how it would work:
Dim mnuMain As clsMenu
'Procedure begin
Set mnuMain = New clsMenu
hFile = mnuMain.SubMenus.Add("File","file")
hFileNew = mnuMain.Submenus.Item("file").Submenus.Add("New","new")
hFileExit = mnuMain.Submenus.Item("file").Submenus.Add("Exit","exit")
'Procedure end
Public Sub WindProc(hWnd As Long, msg As Long, wParam As Long, lParam As Long)
If msg = MENU Then
Select Case wParam
Case hFile
'Call whatever you need when hFile is clicked
Case hFileNew
'Call new code...
Case hFileExit
'Call exit code
End Select
End If
CallWindowProc(hWnd, msg, wParam, lParam)
End Sub
(remember, I'm writing this from outside the IDE ;))
This would be the basic format, since the AddressOf operator cannot be used to call VB functions from within VB.
How does this sound?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.