Avatar of boycoder
boycoder

asked on 

treeview example

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
DelphiPascal

Avatar of undefined
Last Comment
systan
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

what do you mean by 'make them show message' ?
Avatar of boycoder
boycoder

ASKER

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
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

Avatar of boycoder
boycoder

ASKER

No i mean, i have programed them inside the items menu, i just need to show the messages when selected.
Avatar of jimyX
jimyX

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

Avatar of jimyX
jimyX

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

Avatar of boycoder
boycoder

ASKER

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
Avatar of boycoder
boycoder

ASKER

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,.
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

Avatar of jimyX
jimyX

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

Avatar of systan
systan
Flag of Philippines image

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
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

ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial

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

Avatar of boycoder
boycoder

ASKER

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

Avatar of boycoder
boycoder

ASKER

SORRY JIM, i didnt see this. Its what i need, thankyou. Thanks to all who helped again, cheers.. and cheers JIM
Avatar of jimyX
jimyX

Have you check what I provided in this post #35764377?
Avatar of jimyX
jimyX

Cheers
Avatar of systan
systan
Flag of Philippines image

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. =)

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....
Avatar of systan
systan
Flag of Philippines image

Node.Text
that's in my project update, but it's to late. lol,  i hope another question like this, lol
Avatar of systan
systan
Flag of Philippines image

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
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo