I want to create the TreeView structure in TMainMenu.

Hi,

    I want to create the TreeView structure in TMainMenu. Any working example will be helpful.
     Thanks.
mhanefitelAsked:
Who is Participating?
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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
 
jimyXCommented:
Do you mean to have submenu items?
MainMenu-and-SubMenu.JPG
0
 
mhanefitelAuthor Commented:
Hi,
      Yes. Whatever the Treeview structure is i want to make the same structure in TMainMenu.
      Thanks in advance.
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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
 
Ephraim WangoyaCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.