Solved

TTreeView question

Posted on 2000-05-13
8
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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