[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I want to create the TreeView structure in TMainMenu.

Posted on 2011-04-30
5
Medium Priority
?
510 Views
Last Modified: 2012-05-11
Hi,

    I want to create the TreeView structure in TMainMenu. Any working example will be helpful.
     Thanks.
0
Comment
Question by:mhanefitel
5 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35496996
Do you mean to have submenu items?
MainMenu-and-SubMenu.JPG
0
 

Author Comment

by:mhanefitel
ID: 35497189
Hi,
      Yes. Whatever the Treeview structure is i want to make the same structure in TMainMenu.
      Thanks in advance.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 35497832
Use this procedure

For example from ButtonClick

procedure TForm1.Button1Click(Sender: TObject);
begin
  TreeView2MainMenu(TreeView1, MainMenu1);
end;
procedure TForm1.TreeView2MainMenu(TreeView: TTreeView; MainMenu: TMainMenu);
var
  TreeNode: TTreeNode;
  MenuItem: TMenuItem;
  procedure ProcessTreeItem(TreeNode: TTreeNode; Item: TMenuItem);
  var
    SubMenuItem: TMenuItem;
  begin
    if (TreeNode = nil) then
      Exit;
    SubMenuItem := TMenuItem.Create(self);
    SubMenuItem.caption := TreeNode.Text;
    Item.add(SubMenuItem);
    // child nodes
    TreeNode := TreeNode.getFirstChild;
    while TreeNode <> nil do
    begin
      ProcessTreeItem(TreeNode, SubMenuItem);
      TreeNode := TreeNode.getnextSibling;
    end;
  end;

(* ProcessTreeItem *)
begin
  TreeNode := TreeView.TopItem;
  while TreeNode <> nil do
  begin
    MenuItem := TMenuItem.Create(self);
    MenuItem.caption := TreeNode.Text;
    MainMenu.Items.add(MenuItem);
    ProcessTreeItem(TreeNode, MenuItem);
    TreeNode := TreeNode.getnextSibling;
  end;
end;

Open in new window

0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 200 total points
ID: 35497991

Try this
procedure TForm3.Button1Click(Sender: TObject);
var
  I: Integer;
  RootNode: TTreeNode;
  procedure BuildMenuItems(ANode: TTreeNode; AParentMenu: TMenuItem);
  var
    Item: TMenuItem;
    J: Integer;
  begin
    Item := TMenuItem.Create(MainMenu1);
    Item.Caption := ANode.Text;
    AParentMenu.Add(Item);
    for J := 0 to ANode.Count - 1 do
      BuildMenuItems(ANode.Item[J], Item);
  end;
begin
  MainMenu1.Items.Clear;
  RootNode := TreeView1.Items[0];
  while RootNode <> nil do
  begin
    BuildMenuItems(RootNode, MainMenu1.Items);
    RootNode := RootNode.getNextSibling;
  end
end;

Open in new window

0
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 800 total points
ID: 35498057
Just to correct the double inserting of the first Item

procedure TForm1.TreeView2MainMenu(TreeView: TTreeView; MainMenu: TMainMenu);
var
  TreeNode: TTreeNode;
  MenuItem: TMenuItem;
  procedure ProcessTreeItem(TreeNode: TTreeNode; Item: TMenuItem);
  var
    SubMenuItem: TMenuItem;
  begin
    if (TreeNode = nil) then
      Exit;
    SubMenuItem := TMenuItem.Create(self);
    SubMenuItem.caption := TreeNode.Text;
    if Assigned(Item) then
      Item.add(SubMenuItem)
    else
      MainMenu.Items.add(SubMenuItem);
    // child nodes
    TreeNode := TreeNode.getFirstChild;
    while TreeNode <> nil do
    begin
      ProcessTreeItem(TreeNode, SubMenuItem);
      TreeNode := TreeNode.getnextSibling;
    end;
  end;

(* ProcessTreeItem *)
begin
  TreeNode := TreeView.TopItem;
  while TreeNode <> nil do
  begin
    ProcessTreeItem(TreeNode, nil);
    TreeNode := TreeNode.getnextSibling;
  end;
end;
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 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