Solved

TTreeView question

Posted on 2000-05-13
8
156 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

746 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

12 Experts available now in Live!

Get 1:1 Help Now