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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to dynamicaly build

Status
Not open for further replies.

Killa

Programmer
Oct 19, 2001
56
US
Is it possible to dynamicaly build popup menu's so that the options on the menu alter dependant what is clicked
 
I don't know if you can add items to a menu at runtime. If you know in advance all of the menu items you'll need, you can set them up during design and then change their Visible properties at runtime. This would effectively add and remove them from the menus.
 
NO the options would not be known at design time.
I think making a form that looks and behaves some what like a menu would be my best best thanks for your input

 
Sure! The simplist way may be to just add/delete the menu item to a hidden menu at run time. Then add in the form's click event:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbKeyRButton Then PopupMenu MainMDI.Menu1 [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
How do you add and delete an item to a menu at runtime ?

displaying a popup menu isn't the problem its adding/deleteing items/options to a menu to be displayed
 
Adding items you don't know about in advance isn't the problem. It's a few simple calls.

No, the problem is responding to the menu items being clicked on.

It is possible, but it's a pain.
 
I would create an indexed Menu and then you can handle it like a controlarray. The clicks can be handled appropriate in respect to the index:
e.g. open a form create a menu MyMen (visible = false) with a Submenu MyMens (enabled = false) with one menuitem with index 1. Add a command1 and a text1.
Add the following code:
Code:
Private Sub Command1_Click()
    If MyMens(1).Enabled = False Then
        MyMens(1).Caption = Text1.Text
        MyMens(1).Enabled = True
    Else
        Load MyMens(MyMens.Count + 1)
        MyMens(MyMens.Count).Caption = Text1.Text
    End If
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    PopupMenu MyMen
End Sub

Hope this helps
Andreas
 
Hi!
Code:
Option Explicit
Const MENU_LOAD_OPTION_A = 1
Const MENU_LOAD_OPTION_B = 2
Const MENU_LOAD_OPTION_C = 3
Const MENU_LOAD_OPTION_D = 4

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbKeyRButton Then
        On Error Resume Next
        Unload idMnu(MENU_LOAD_OPTION_B)
        Unload idMnu(MENU_LOAD_OPTION_C)
        Load idMnu(MENU_LOAD_OPTION_A)
        Load idMnu(MENU_LOAD_OPTION_D)
        idMnu(MENU_LOAD_OPTION_A).Caption = "Option A"
        idMnu(MENU_LOAD_OPTION_D).Caption = "Option D"
        idMnu(MENU_LOAD_OPTION_A).Enabled = True
        idMnu(MENU_LOAD_OPTION_D).Enabled = True
        idMnu(0).Enabled = False
        PopupMenu idMain
    End If
End Sub

Private Sub Command2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbKeyRButton Then
        On Error Resume Next
        Unload idMnu(MENU_LOAD_OPTION_A)
        Unload idMnu(MENU_LOAD_OPTION_D)
        Load idMnu(MENU_LOAD_OPTION_B)
        Load idMnu(MENU_LOAD_OPTION_C)
        idMnu(MENU_LOAD_OPTION_B).Caption = "Option B"
        idMnu(MENU_LOAD_OPTION_C).Caption = "Option C"
        idMnu(MENU_LOAD_OPTION_B).Enabled = True
        idMnu(MENU_LOAD_OPTION_C).Enabled = True
        idMnu(0).Enabled = True
        PopupMenu idMain
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set Form1 = Nothing
End Sub

NOTES:
1) You don't "create", you only copy a menu item (array type) and set its properties, especially the Caption. Then, a Select Case or similar conditional statements to validate each index will execute the corresponding codes.
2) You might add error handlers when trying to validate if a menu object is already loaded.
3) You can't unload menu items added at design mode.

This applies with VB native. I've seen API samples that actually create/draw menu items, but it's bloody indeed.

[peace]
 
Thanks Guys

It works wonderfully

Killa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top