Solved

Adding menu in TreeView

Posted on 1998-10-14
5
174 Views
Last Modified: 2010-04-30
Hi,

I want to add a context menu in a treeview. The menu will vary depending on the item I will click on (like in the Windows Explorer). I have found no reference to do that. Anyone can give mee a clue?
0
Comment
Question by:pjroy
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
vmano earned 30 total points
Comment Utility
check out help on PopupMenu method. good explanation and examples.
0
 
LVL 2

Expert Comment

by:shchuka
Comment Utility
There are two possible solutions:

1. (Not the most elegant, but the easiest to implement).  Create several menues at design time and at run time in MouseDown event of the TreeView decide which one to display.

2. (A lot more elegant, but requires more programming.)  Create only one menu, say mnuPopup.  Create one item under it with empty caption, name it mnuDynamic (for examle) and set its index to 0, so that its a menu control array.  Also set this item's visible to false.  At run time, determine what items you need and do the following (suppose, the array MenuCaptions(1 to n) contains Captions of the menu items you need):

dim i as integer
for i=1 to n
    load mnuDynamic(i-1)
    mnuDynamic(i).caption = MenuCaptions(i)
    mnuDynamic(i).visible = true
next i

In the click event of mnuDynamic you will need to analyze the caption of the item clicked and do the stuff that you need to do.  After that, unload the items again:

private sub mnuDynamic_Click(index as integer)
    dim capt as string
    dim i as integer

    capt = mnuDynamic(index)
    if capt = val1 then
        ' Do something here
    elseif capt = val2 then
        ' Do something here
...
    end if

    for i=ubound(mnuDynamic) to 0 step -1
        mnuDynamic(i).visible=false
        unload mnuDynamic(i)
    next i
end sub

And finally in the MouseDown event of the TreeView you'll check what items you need to have in the menu, create them and do
    PopupMenu mnuPopup

Also you need to check if the menu was canceled and unload the items.  I believe you can do that from MouseUp event of the TreeView.

Hope this helps,
-Aleks.
0
 

Author Comment

by:pjroy
Comment Utility
Adjusted points to 30
0
 

Author Comment

by:pjroy
Comment Utility
How do I unload a menu loaded with PopupMenu?

When I right click at another place, I want the menu to be closed and reopened a new one.
0
 
LVL 3

Expert Comment

by:vmano
Comment Utility
When you right click, just specify the X, Y coordinates for the popupmenu. this will take care when you click at another place.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now