treeview example

boycoder
boycoder used Ask the Experts™
on
I have have a treeview, can someone show me an example to get started.. here is what i have..

1 new item > 2 sub item
1 new item > 1 sub item

so its like this..

+
MAIN
SUB
SUB
MAIN
SUB


just make them show message on each one so i can understand it.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
what do you mean by 'make them show message' ?

Author

Commented:
i mean ,
information is NEW ITEM
TEST is sub item

if treeview1.selected :=('Information')
hen begin
showmessage('MAIN - information);
end
else begin
if treeview1.selected :=('test')
then begin
showmessage('SUB - test');
end
else begin
if treeview1.selected :=('test2')
then begin
showmessage('SUB - test2');
end
else begin
if treeview1.selected :=('second')
then begin
showmessage('MAIN - 2');
end;


like this.. but with corrected code,. cheers



just so i can view a MESSAGE on clicking the nodes SUB and MAIN
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
if you mean how to create nodes programmatically :
var
 Node, Node2:TTreeNode;
begin
 Node:=aTreeView.Items.AddChild(nil, 'MAIN1');
 Node.ImageIndex:=0; // for example, set an image (using aTreeView.Images)
 Node2:=aTreeView.Items.AddChild(Node, 'SUB1');
 Node2.ImageIndex:=1; // another image for sub items
 Node2:=aTreeView.Items.AddChild(Node, 'SUB2');
 Node2.ImageIndex:=1; // another image for sub items
 Node:=aTreeView.Items.AddChild(nil, 'MAIN2');
 Node.ImageIndex:=0;
 Node2:=aTreeView.Items.AddChild(Node, 'SUB1');
 Node2.ImageIndex:=1; // another image for sub items
end;

Open in new window

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
No i mean, i have programed them inside the items menu, i just need to show the messages when selected.
Top Expert 2011

Commented:
If I understood you correctly, at the Treeview OnClick add this code:
procedure TForm1.TreeView1Click(Sender: TObject);
begin
  if TreeView1.Selections[0].Level = 0 then
    ShowMessage('Main '+ TreeView1.Selections[0].Text)
  else
    ShowMessage('Sub '+ TreeView1.Selections[0].Text);
end;

Open in new window

Top Expert 2011

Commented:
In case you have many selected and you need to show every one of them:
procedure TForm1.TreeView1Click(Sender: TObject);
var
  i:integer;
begin
  if TreeView1.SelectionCount > 0 then
    begin
      //showmessage('There are '+ IntToStr(TreeView1.SelectionCount) + ' items selected');
      for i:= 0 to TreeView1.SelectionCount-1 do
        begin
          if TreeView1.Selections[i].Level = 0 then
            ShowMessage('Main '+ TreeView1.Selections[i].Text)
          else
            ShowMessage('Sub '+ TreeView1.Selections[i].Text);
        end;
    end;
end;

Open in new window

Author

Commented:
hi can you guys make it simple for me if possible as i have to ad so so so many items to the treeview..

So if you can set the code like:

information is NEW ITEM
TEST is sub item

if treeview1.selected :=('Information')
hen begin
showmessage('MAIN - information);
end
else begin
if treeview1.selected :=('test')
then begin
showmessage('SUB - test');
end
else begin
if treeview1.selected :=('test2')
then begin
showmessage('SUB - test2');
end
else begin
if treeview1.selected :=('second')
then begin
showmessage('MAIN - 2');
end;


it would be great cheers

Author

Commented:
information is NEW ITEM
TEST is sub item

if treeview1.selected :=('Information')
hen begin
form1.show;
end
else begin
if treeview1.selected :=('test')
then begin
form2.show
end
else begin
if treeview1.selected :=('test2')
then begin
form3.show
end

end;


IDEAL what want it is selcted item 1 then.... and if selected item 2.. then.. thats much better if possible,.
Ephraim WangoyaSoftware Engineer

Commented:
try this
procedure TForm1.TreeView1Click(Sender: TObject);
var
  Path: String;
  function GetNodePath(ANode: TTreeNode): string;
  var
    Node: TTreeNode;
  begin
    Result := ANode.Text;
    Node := ANode.Parent;
    while Node <> nil do
    begin
      Result := Node.Text + '->' + Result;
      Node := Node.Parent;
    end;
  end;
begin

  if Assigned(TreeView1.Selected) then
    Path := GetNodePath(TreeView1.Selected);
  ShowMessage(Path);
end;

Open in new window

Top Expert 2011

Commented:
Do you set the MultiSelect property to True?
If yes how do you want to handle that then? Do you want to show all the selected forms?
procedure TForm1.TreeView1Click(Sender: TObject);
var
  i:integer;
begin
  for i:= 0 to TreeView1.SelectionCount-1 do
    begin
      if TreeView1.Selections[i].Text = 'Information' then
        form1.show
      else
        if TreeView1.Selections[i].Text = 'test' then
          form2.show
        else
          if TreeView1.Selections[i].Text = 'test2' then
            form3.show;
   end;
end;

Open in new window

Commented:
Let me try;
 treeviewtests.ZIP

the following project treeviewtests has the capability to add main node, add sub node, delete main node, delete sub node,  and also during on change node value, it displays the current node.

good luck
Ephraim WangoyaSoftware Engineer

Commented:
The OnChange event seems to be much better for you
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  Path: String;
  function GetNodePath(ANode: TTreeNode): string;
  var
    Node: TTreeNode;
  begin
    Result := ANode.Text;
    Node := ANode.Parent;
    while Node <> nil do
    begin
      Result := Node.Text + '->' + Result;
      Node := Node.Parent;
    end;
  end;
begin
  if Assigned(TreeView1.Selected) then
    Path := GetNodePath(TreeView1.Selected);
  ShowMessage(Path);
end;

Open in new window

Top Expert 2011
Commented:
If MultiSelect is False then:
procedure TForm1.TreeView1Click(Sender: TObject);
begin
  if TreeView1.Selections[0].Text = 'Information' then
    form1.show
  else
    if TreeView1.Selections[0].Text = 'test' then
      form2.show
    else
      if TreeView1.Selections[0].Text = 'test2' then
        form3.show;
end;

Open in new window

Ephraim WangoyaSoftware Engineer

Commented:

If you want to use this to show forms, yo need a different approach. Don't use the node text for comparison otherwise if you change the text then you have to change your code as well.

Add the form pointer itself to the node and then when the node is clicked, you simply display whats referenced by the node.
eg

when adding the node, use this

 
Treeview1.Items.AddObject(nil, 'Information', Form1);
 Treeview1.Items.AddObject(nil, 'Test', Form2);
 Treeview1.Items.AddObject(nil, 'Test2', Form3);

Open in new window


Then for onchange event, do this

 
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  if Assigned(Node.Data) and (TObject(Node.Data) is TForm) then
    TForm(Node.Data).Show;
end;

Open in new window

Author

Commented:
hI thanks allof you but maybe i am not being clear as i do not make myself clear alot.

If i select the first item of the treeview called "itest1".. i want to show form1
if i select the second item of the treeview called "test2" i want to show form2
if i select the third item of the treeview called "test3" i want to show form3
if i select the fourth item of the treeview called "test4" i want to show form4


/// TEST1 is a NEW ITEM
/// TEST2 is a sub item of TEST1
/// TEST3 is a sub item again of TEST1
/// TEST4 is a another NEW ITEM


----1
--2
--3
----3

Author

Commented:
SORRY JIM, i didnt see this. Its what i need, thankyou. Thanks to all who helped again, cheers.. and cheers JIM
Top Expert 2011

Commented:
Have you check what I provided in this post #35764377?
Top Expert 2011

Commented:
Cheers

Commented:
lol, that's not a general use of treeview, it was made manually. lol,  Am I right?
I have an update, but I think I'm late. =)
Ephraim WangoyaSoftware Engineer

Commented:

Why do people insist on bad habits?

Sometimes its good to take your time and apply sound principles.

I clearly state in my post (35764394) why not to use the Node.Text for comparison. So now your Node caption is tightly linked with your form, ohhh

That is just bad coding and terrible to maintain. Well if it works for you now....

Commented:
Node.Text
that's in my project update, but it's to late. lol,  i hope another question like this, lol

Commented:
my first comment,  #35764361
and also during on change node value, it displays the current node.

ewangoya's comment, #35764362
The OnChange event seems to be much better for you

here's my sample2 project update for simple treeview, with ewangoya on change.
 treeviewtest2.ZIP

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial