CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 1999
    Location
    India , Hyserabad
    Posts
    2

    Adding Menu Item at Runtime

    How Can I Add and Remove Menu ( Submenu ) at Run-Time in VB.

    there are some API Functions for Adding Menus and Changing the Properties of Menus.



    Regards,
    Anil Gopu

  2. #2
    Join Date
    Apr 1999
    Posts
    21

    Re: Adding Menu Item at Runtime

    You must create the basic menu structure at design time.

    The menu should contain a sub-menu (can be non-visible) item with a non-empty index (like "0"); this creates a menu (control) array.

    At run-time, you can Load additional items to the sub-menu

    Example: Cut/paste below to a file named Form1.frm; Create a new project and add the form to the project.


    VERSION 4.00
    Begin VB.Form Form1
    Caption = "Form1"
    ClientHeight = 7260
    ClientLeft = 1140
    ClientTop = 3000
    ClientWidth = 6690
    Height = 7950
    Left = 1080
    LinkTopic = "Form1"
    ScaleHeight = 7260
    ScaleWidth = 6690
    Top = 2370
    Width = 6810
    Begin VB.CommandButton Cmd1
    Caption = "Command 1"
    Height = 555
    Left = 1020
    TabIndex = 0
    Top = 300
    Width = 1575
    End
    Begin VB.Label Label1
    Caption = "Label1"
    Height = 375
    Left = 1020
    TabIndex = 1
    Top = 1020
    Width = 1935
    End
    Begin VB.Menu mnuMain
    Caption = "Main"
    Begin VB.Menu mnuSubOfMain
    Caption = "SubTop"
    Enabled = 0 'false
    Index = 0
    Visible = 0 'false
    End
    Begin VB.Menu Sep1
    Caption = "-"
    End
    Begin VB.Menu mnuP2
    Caption = "Part 2"
    End
    End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_Creatable = false
    Attribute VB_Exposed = false
    option Explicit


    Dim frmiMenuNdx as Integer
    Dim frmiMenuAdd as Integer
    Dim frmiMenuMax as Integer
    private Sub Cmd1_Click()
    Dim locIa as Integer
    Dim locIn as Integer
    locIa = frmiMenuAdd
    locIn = frmiMenuNdx
    If locIa then
    locIn = locIn + 1
    If (locIn > frmiMenuMax) then
    locIa = false
    locIn = frmiMenuNdx - 1
    End If
    else
    locIn = locIn - 1
    If (locIn < 1) then
    locIa = true
    locIn = frmiMenuNdx + 1
    End If
    End If
    If locIa then
    Load mnuSubOfMain(locIn)
    mnuSubOfMain(locIn).Visible = true
    mnuSubOfMain(locIn).Enabled = true
    mnuSubOfMain(locIn).Caption = "Item " & (locIn)
    Label1.Caption = "Added: #" & locIn
    else
    Unload mnuSubOfMain(frmiMenuNdx)
    Label1.Caption = "Removed: #" & frmiMenuNdx
    End If

    frmiMenuAdd = locIa
    frmiMenuNdx = locIn
    End Sub

    private Sub Form_Load()
    frmiMenuNdx = 0
    frmiMenuAdd = true
    frmiMenuMax = 8
    Label1.Caption = ""
    Visible = true
    Cmd1_Click
    End Sub





    Good luck,
    Kevin



  3. #3
    Join Date
    Apr 1999
    Posts
    21

    Re: Adding Menu Item at Runtime

    You must create the basic menu structure at design time.

    The menu should contain a sub-menu (can be non-visible) item with a non-empty index (like "0"); this creates a menu (control) array.

    At run-time, you can Load additional items to the sub-menu

    Example: Cut/paste below to a file named Form1.frm; Create a new project and add the form to the project.


    VERSION 4.00
    Begin VB.Form Form1
    Caption = "Form1"
    ClientHeight = 7260
    ClientLeft = 1140
    ClientTop = 3000
    ClientWidth = 6690
    Height = 7950
    Left = 1080
    LinkTopic = "Form1"
    ScaleHeight = 7260
    ScaleWidth = 6690
    Top = 2370
    Width = 6810
    Begin VB.CommandButton Cmd1
    Caption = "Command 1"
    Height = 555
    Left = 1020
    TabIndex = 0
    Top = 300
    Width = 1575
    End
    Begin VB.Label Label1
    Caption = "Label1"
    Height = 375
    Left = 1020
    TabIndex = 1
    Top = 1020
    Width = 1935
    End
    Begin VB.Menu mnuMain
    Caption = "Main"
    Begin VB.Menu mnuSubOfMain
    Caption = "SubTop"
    Enabled = 0 'false
    Index = 0
    Visible = 0 'false
    End
    Begin VB.Menu Sep1
    Caption = "-"
    End
    Begin VB.Menu mnuP2
    Caption = "Part 2"
    End
    End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_Creatable = false
    Attribute VB_Exposed = false
    option Explicit


    Dim frmiMenuNdx as Integer
    Dim frmiMenuAdd as Integer
    Dim frmiMenuMax as Integer
    private Sub Cmd1_Click()
    Dim locIa as Integer
    Dim locIn as Integer
    locIa = frmiMenuAdd
    locIn = frmiMenuNdx
    If locIa then
    locIn = locIn + 1
    If (locIn > frmiMenuMax) then
    locIa = false
    locIn = frmiMenuNdx - 1
    End If
    else
    locIn = locIn - 1
    If (locIn < 1) then
    locIa = true
    locIn = frmiMenuNdx + 1
    End If
    End If
    If locIa then
    Load mnuSubOfMain(locIn)
    mnuSubOfMain(locIn).Visible = true
    mnuSubOfMain(locIn).Enabled = true
    mnuSubOfMain(locIn).Caption = "Item " & (locIn)
    Label1.Caption = "Added: #" & locIn
    else
    Unload mnuSubOfMain(frmiMenuNdx)
    Label1.Caption = "Removed: #" & frmiMenuNdx
    End If

    frmiMenuAdd = locIa
    frmiMenuNdx = locIn
    End Sub

    private Sub Form_Load()
    frmiMenuNdx = 0
    frmiMenuAdd = true
    frmiMenuMax = 8
    Label1.Caption = ""
    Visible = true
    Cmd1_Click
    End Sub





    Good luck,
    Kevin



  4. #4
    Join Date
    Sep 1999
    Location
    India , Hyserabad
    Posts
    2

    Re: Adding Menu Item at Runtime

    Is there any tech. to add this Menus using the APIs so that I need not Declear the Demmy Elements first. Using this Tech. We are Limited to a Fixed no of Menu items in our Program. The Addition and Removal of the Menu Items will be done By the user at runtime. In APIs there is Something called as AddMenu and others. And Slo I need to Add ICONs to the Menus. Is there any tech.

    Regards,
    Anil Gopu

  5. #5
    Join Date
    Apr 1999
    Posts
    21

    Re: Adding Menu Item at Runtime

    AnilGopu-

    I have prepared a document listing most of the Menu API's. Please send me your
    email address so I can email it to you. My email address is [email protected]

    Keep in mind, that this will allow you to add and delete menu items at run-time, attaching
    code (events) to these menu items may be tricky.

    Windows sends messages to the parent form when a user clicks on a menu item.
    You will probably have to imploy a message-hook to intercept these messages,
    and then decode them, in order to do anything useful with the menus you create in
    this way.

    I would still recommend that you menu-control-arrays and get creative in how
    you use Load/Unload for these menu-items at run-time.

    Bye
    Kevin




  6. #6
    Join Date
    Apr 1999
    Posts
    21

    Re: Adding Menu Item at Runtime

    AnilGopu-

    I have prepared a document listing most of the Menu API's. Please send me your
    email address so I can email it to you. My email address is [email protected]

    Keep in mind, that this will allow you to add and delete menu items at run-time, attaching
    code (events) to these menu items may be tricky.

    Windows sends messages to the parent form when a user clicks on a menu item.
    You will probably have to imploy a message-hook to intercept these messages,
    and then decode them, in order to do anything useful with the menus you create in
    this way.

    I would still recommend that you menu-control-arrays and get creative in how
    you use Load/Unload for these menu-items at run-time.

    Bye
    Kevin




  7. #7
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Adding Menu Item at Runtime

    As you want to be able to add icons to your menus and (possibly) create multiple levels of menus at run-time, I'd recommend you download the excellent PopupMenu control from vbAccelerator - you get all of the source-code for this control and for the subclassing DLL that it uses.

    I've been using it for months and only found a couple of bugs which Steve assures me have been fixed in the latest downloadable version.

    Take a look at http://www.vbaccelerator.com/codelib...nu/article.htm

    You might want to check out the Rebar/Toolbar control page also - it shows how to have these menus in a rebar band and associated with toolbar buttons.


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  8. #8
    Join Date
    Aug 1999
    Location
    France
    Posts
    39

    Re: Adding Menu Item at Runtime

    You can use the API
    SetMenu: to assigns a new menu to a window
    SetMenuItemInfo:to change information about a menu
    ...
    GetMenu: to retrieve the handle of a windows
    ...



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

Featured