Olympus
asked on
TTreeView question
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
--olympus
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
you must check to see if Selected<>nil before doing anything with it.
Alex
ASKER
how do i distinguish different items then?
just doing a large if-then statement group checking .Caption?
just doing a large if-then statement group checking .Caption?
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
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
Are there categorie-specific parameters for the associated procedures ?
If yes, another way would be useful.
If yes, another way would be useful.
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(Se nder: TObject);
var
ParentNode : TTReeNode;
begin
ParentNode := treeview1.items.add(nil,'P arent Node');
Treeview1.Items.addChildOb ject(Paren tNode,'Com mand 1',Pointer(COMMAND_1));
Treeview1.Items.addChildOb ject(Paren tNode,'Com mand 2',Pointer(COMMAND_2));
Treeview1.Items.addChildOb ject(Paren tNode,'Com mand 3',Pointer(COMMAND_3));
end;
procedure TForm1.TreeView1Click(Send er: 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.
--------------------------
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(Se
var
ParentNode : TTReeNode;
begin
ParentNode := treeview1.items.add(nil,'P
Treeview1.Items.addChildOb
Treeview1.Items.addChildOb
Treeview1.Items.addChildOb
end;
procedure TForm1.TreeView1Click(Send
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.
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER