Solved

TTreeView question

Posted on 2000-05-13
8
159 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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