Using popup menu in TTreeview

Posted on 2009-02-10
Last Modified: 2012-05-06
Hi! I want to know how to use popup menu in TreeView. Let's say i create nodes at runtime and after i have 1 root node and 5 subnodes. So i want popup menu to show when user select any of these subnodes buy clicking right mouse button. The content of popmenu may be static or dynamic (for example contains the caption of the selected node). Obviously, popup menu may show only if right button clicked on selected node. Popup menu cannot show if right click over treeview canvas. Please kindly help
Question by:Alexander_Savenko
    LVL 6

    Accepted Solution

    here is the way how I use popup menu

    procedure TForm1.TreeViewMouseDown(Sender: TObject;
              Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
       tpt : TPoint;
       Node: TTreeNode;
       tpt.X := X;
       tpt.Y := Y;
       Node := TreeView.GetNodeAt(tpt.X, tpt.Y);
       if (Node = nil) or
          (length(trim(Node.Text)) = 0)  then   Exit;
       if Button = mbRight then
          PopupMenu.Popup(X+(Sender as TTreeView).ClientOrigin.X+30, Y+(Sender as TTreeView).ClientOrigin.Y-8);

    Author Comment

    Ok, very useful. And how to manage items in Popupmenu at runtime. Let's say i need to add first item as Node caption, after line break and 1 submenu with 3 items to select?
    LVL 6

    Expert Comment


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now