Solved

Adding menu in TreeView

Posted on 1998-10-14
5
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

719 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