Solved

Adding menu in TreeView

Posted on 1998-10-14
5
175 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
ID: 1439918
check out help on PopupMenu method. good explanation and examples.
0
 
LVL 2

Expert Comment

by:shchuka
ID: 1439919
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
ID: 1439920
Adjusted points to 30
0
 

Author Comment

by:pjroy
ID: 1439921
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
ID: 1439922
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

919 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

14 Experts available now in Live!

Get 1:1 Help Now