Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TTreeView question

Posted on 2000-05-13
8
Medium Priority
?
164 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 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