Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding menu in TreeView

Posted on 1998-10-14
5
Medium Priority
?
196 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 120 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

650 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