Solved

TTreeView question

Posted on 2000-05-13
8
157 Views
Last Modified: 2010-04-04
What would be a good method of associating procedures with the clicks of items on a treeview? I am using a tree view as a list of commands in their own categories.  i need to make it so if these commands (subitems) are clicked on, some code is ran.  what would be the best/most efficient method to do so?

--olympus
0
Comment
Question by:Olympus
8 Comments
 

Author Comment

by:Olympus
ID: 2807687
Adjusted points from 0 to 50
0
 
LVL 15

Expert Comment

by:simonet
ID: 2807755
Use the OnClick event of the TTreeView and the check for the Selected item (TTreeView.Selected returns the currently selected tree node).

you must check to see if Selected<>nil before doing anything with it.

Alex
0
 

Author Comment

by:Olympus
ID: 2807793
how do i distinguish different items then?
just doing a large if-then statement group checking .Caption?
0
 
LVL 15

Expert Comment

by:simonet
ID: 2807854
Unfortunatly yes. :(

Another option is to use the Index for each item and then do a CASE testing (if you know what each item's index mean and they are static or your code can respond to changes in each item's index).

Alex
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:Freaky
ID: 2808909
Are there categorie-specific parameters for the associated procedures ?
If yes, another way would be useful.
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2808991
Here's one way to do it. Associaate a value with the TreeNode's data pointer, and case that. It's not the normal use for it, bit it works, and it's not dependant on node captions or indices.

----------------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    btnAddNodes: TButton;
    Button2: TButton;
    procedure btnAddNodesClick(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure setcaption;
    { Public declarations }
  end;

var
  Form1: TForm1;

Const
  COMMAND_1 = 1;
  COMMAND_2 = 2;
  COMMAND_3 = 3;

implementation

{$R *.DFM}

procedure TForm1.setcaption;
begin
end;

procedure TForm1.btnAddNodesClick(Sender: TObject);
var
  ParentNode : TTReeNode;
begin
  ParentNode := treeview1.items.add(nil,'Parent Node');
  Treeview1.Items.addChildObject(ParentNode,'Command 1',Pointer(COMMAND_1));
  Treeview1.Items.addChildObject(ParentNode,'Command 2',Pointer(COMMAND_2));
  Treeview1.Items.addChildObject(ParentNode,'Command 3',Pointer(COMMAND_3));
end;

procedure TForm1.TreeView1Click(Sender: TObject);
begin
  if Treeview1.selected = nil then exit;
  with Treeview1 do
    case Integer(Selected.Data) of
      1 :Showmessage('command 1');
      2 :Showmessage('command 2');
      3 :Showmessage('command 3');
    end;
end;

end.
0
 

Author Comment

by:Olympus
ID: 2809031
no there are no category specific parameters, i have a TreeView as a list of different commands categorized by folders to make it more understandable. i need each of these commands do do a specific task, and the ability of more commands being added during runtime.
0
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2809794
Even better if there are no parameters (but with parameters it would only be marginally more difficult). Just use the function/procedure address in the node's data property (provided they are not part of a class) like:

procedure Procedure1;
begin
end;

Treeview1.Items.addChildObject(ParentNode, 'Command 1', @Procedure1);


procedure TForm1.TreeView1Click(Sender: TObject);

var
  Command: TProcedure;
  Node: TTreeNode;

begin
  Node := TreeView1.Selected;
  if Assigned(Node) then
  begin
    Command := Node.Data;
    if Assigned(Command) then Command;
  end;
end;

As you can see here no big case statements are needed. Only if you need different parameters then you need to use a case statement to distinguish between the procedure groups with different parameters.

Ciao, Mike
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

17 Experts available now in Live!

Get 1:1 Help Now