Hi,
How can I add a menu and delete a menu at run time?
An example would be appreciated..
Dan
Printable View
Hi,
How can I add a menu and delete a menu at run time?
An example would be appreciated..
Dan
The best way is by using the api.
(Maybe this example does what you want)
Use AppendMenu to add and RemoveMenu to remove.
Code:Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub Command1_Click()
'Add a menu
AppendMenu GetSubMenu(GetMenu(hwnd), 0), MF_BYPOSITION, 0, "New Item"
End Sub
Private Sub Command2_Click()
'Remove a menu
RemoveMenu GetSubMenu(GetMenu(hwnd), 0), 0, MF_BYPOSITION
End Sub
Is the only way to do it through the API?
The reason I ask is that one of the skills measured for the MCSD Certification is to be able to:
"Create an application that adds and deletes menus at run time."
I'd be really suprised if they expect you to work with the API. In fact, the course that I took did not ever talk about this subject..
Is there a way to do this entirely within VB without calling the API? I just want to be prepared for the exam.
thanks,
Dan
Simple way is to load and unload during runtime. You must have 1 menuitem at the same level present and you cant add new subitems to a menuitem.
I used this code to add submenu's with page numbering. One menuitem must be there with index 0
It's no good this way, API code is much better but probably not what you need to know for your course.Code:Public Sub procSetPageNr()
On Error Resume Next
Dim giCounter as Integer
'clear excisting pages
If mnuGoBarToPageNr.Count > 1 Then
For giCounter = mnuGoBarToPageNr.Count To 1
Unload frmMain.mnuGoBarToPageNr(giCounter)
Next giCounter
End If
'ad new items
For giCounter = 2 To giPageCounter
Load frmMain.mnuGoBarToPageNr(giCounter)
mnuGoBarToPageNr(giCounter).Visible = True
mnuGoBarToPageNr(giCounter).Caption = "Page" & giCounter
Next giCounter
End Sub