Results 1 to 5 of 5

Thread: Add/Delete menu at run time?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    How can I add a menu and delete a menu at run time?

    An example would be appreciated..

    Dan


  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    The best way is by using the api.
    (Maybe this example does what you want)
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  3. #3
    Guest
    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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

  5. #5
    Member
    Join Date
    Oct 2000
    Location
    Netherlands
    Posts
    54

    Wink Load and Unload

    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

    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
    It's no good this way, API code is much better but probably not what you need to know for your course.
    A mind is like a parachute, it has to open to let it work
    www.2beesoft.com for Icon Manager with over 20.000 free icons
    VB6 Ent. SP4, ASP, W2000/W98

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width