Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I make and add items to a sub-menu? 1

Status
Not open for further replies.

sarahmc22

Programmer
Nov 30, 2000
36
0
0
US
I need to add a menu item which will contain a sub-menu to which I will need to add items at runtime. Like the Recent Projects item below:



File
Open
Recent Projects > Project1
Project2
Exit



Any ideas how to do this?

S
 
I need to add them programmatically, i.e., when I query the DB to see which project files were last opened, I need to add them programmatically to the sub-menu.

S
 
There are 2 ways to do this. The first uses the AppendMenu or InsertMenu APIs (If you'd like an example of these, let me know and I will post them). The other method (and the simpler one) treat the menu items just like a control array.
Basically, to use the control array method, you need to create a "template menu item" at design time and make it a control array by making its index = 0. Also, this menu item should have its visible property set to false (this can be tricky since at least one sub menuitem MUST be visible . . . to overcome this, I would put a "label menu" that is just text header such as "Recent Projects). After that is completed, you simply have to load new menu items (with a unique index value) as need and then set its properties. For example . . .


for intRecentProjectIndex = 1 to 5
Load mnuRecentProject(intRecentProjectIndex)

with mnuRecentProject(intRecentProjectIndex)
.Caption = "Recent Project XYZ"
.Visible = True
End with
next intRecentProjectIndex


This is a very simple bit of code . . . if you'd like more detail, just ask, but this should be enough to get you started. Also, if you want more control, there are the APIs that I mentioned (and can demonstrate for you if needed), but they tend to raise other complications.



- Jeff Marler B-)
 
I got the sub menu to work, only I'm not sure how I handle events from the submenu items. Neither sub mnuRecentProjectsSubItem(0)_Click() nor sub mnuRecentProjectsSubItem_Click() seem to work.

this must be simple, right?

S
 
Hi there, sarahmc22.
You might have already figured this one out, but here is what I did to obtain the results that I think you want.
I used Jmarler's example with the form and the code, but modified it to this:

'this project will put recent files in the menu
'recent.rec is the file where the last 5 opened files are
'stored. you can name it whatever you want.
'I am calling the form that creates the menus on startup,
'but you can probably do this routine anywhere, like after
'you open a file, to add it to the recent list.

Dim a(5) 'select maximum to display (also below)
Sub form_load()
Open "C:\recent.rec" For Input As #1
Do Until EOF(1)
If intrecentprojectindex = 5 Then Exit Do intrecentprojectindex = intrecentprojectindex + 1
Input #1, a(intrecentprojectindex)
Load recentproject(intrecentprojectindex)
With recentproject(intrecentprojectindex)
.Caption = a(intrecentprojectindex)
.Visible = True
End With
Loop
Close 1
End Sub

'this sub prints out which was clicked on.

Private Sub recentproject_click(index As Integer)
Print a(index)
End Sub
________________________________________________________
Instead of printing the file you select in the form, you can use it in the open command to open the file.
Hope this helps.
 
Im sorry, I should have previewed that last post before posting it.
The 3 lines after the OPEN command should look like this:

Do Until EOF(1)
If intrecentprojectindex = 5 Then Exit Do
intrecentprojectindex = intrecentprojectindex + 1
Input #1, a(intrecentprojectindex)
 
SkennyR

You do realise this thread is nearly 4 years old, dont you?
 
>Mar 27, 2001
>Jan 26, 2005 ... [y]ou might have already figured this one out ...

Chortle
 
Yes AndyLee, I knew that, but I was having the same problem, and after searching for the answer I only found this thread. But since no one answered SarahMC22's last post, I thought I would throw in my solution, just in case someone else comes across the same problem.
Even late help is appreciated when it is really needed.
I sure hope the vast amount of information on this website doesnt ever become obsolete.

StrongM, what does Chortle mean?
Have a nice day, and Happy Programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top