Solved

Tree View Creating simple Menu

Posted on 2006-07-13
9
222 Views
Last Modified: 2012-05-05
Hi

I am struggeling with a simple tree view, I look throw some examples on previous questions answered..but still struggeling...

I want to create a menu with a tree view. Here is a example of my menu...
.... a * indicate a parent, and this parent has children, if a click a parent it must expand all its children.
IF a parent has children then the parent must not call any sub program, it must act only as the parent nothing else.
BUT if a item in my treeview has no children then it  must act as a child and must call a subprogram..

E.G.....

A
*B
     b1
     b2
C
*D
   d1
   d2
E
F

(NOW if a click on B then B must show it children and b1,b2,b3 must call a program when i click on it)
(If a click on A or C or E or F then these nodes  must call a program)

*** B must not do anything it must just expand......

I use the unique index here is a exapmle of my code....

procedure TfrmMainMenu.tvAdminChange(Sender: TObject; Node: TTreeNode);
   begin
      case tvAdmin.Selected.AbsoluteIndex of
      0 : LoadApplication('prjAlterOwnerDetails.exe');
      1 : LoadApplication('prjManTimeShareUsage.exe');
      3 : LoadApplication('prjEnqUnitWeek.exe');
      4 : LoadApplication('prjEnqLevyAcc.exe');
      5 : LoadApplication('prjExcGuestLookup.exe');
end

end;

**** MY PROBLEM IS Sometimes when a click on a parent the + sign to expand it, then it calls the onchange event and calls the first item in my treeview....
AND THIS I DONT KNOW HOW TO FIX OR HOW TO USE THE TREE VIEW CORRECTLY..

please help me how to get this tree view to work correctly on the correct item which I CLICK.
0
Comment
Question by:henryreynolds
  • 5
  • 4
9 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17098265
this seems to work ok:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Timer1: TTimer;
    procedure TreeView1Collapsing(Sender: TObject; Node: TTreeNode;
      var AllowCollapse: Boolean);
    procedure TreeView1Expanding(Sender: TObject; Node: TTreeNode;
      var AllowExpansion: Boolean);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
  private
    { Private declarations }
    doexp,
    docol:boolean;
    procedure doit;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.TreeView1Collapsing(Sender: TObject; Node: TTreeNode;
  var AllowCollapse: Boolean);
begin
  if docol then AllowCollapse:=false;
end;

procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
  var AllowExpansion: Boolean);
begin
  if doexp then AllowExpansion:=false;
end;

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  doit;
end;

procedure tform1.doit;
begin
  if treeview1.Selected.HasChildren then
  begin
    if treeview1.Selected.Expanded then
    begin
      doexp:=true;
      treeview1.Selected.Collapse(true);
      doexp:=false;
    end                            else
    begin
      docol:=true;
      treeview1.Selected.Expand(true);
      docol:=false;
    end;
  end                               else
    showmessage('here execute whatever')
end;

end.
0
 

Author Comment

by:henryreynolds
ID: 17098406
Hi thanx for your quick help, it almost working 100% the only problem is as follows.

when the form starts up and their are no focus on the treeview.....WHEN I FIRST CLICK ON THE EXPAND BUTTON, then it call the first item on my treeview.

sorry i hope you understand, i try again..

when the form shows, and I go and click on the Parent the + sign, then I dont why but its go and call the first item on my tree view.

but after that its 100%,,,, it only the first click on the +

Thanx I hope you understand

Henry
0
 
LVL 28

Expert Comment

by:2266180
ID: 17098560
that is normal behaviour. when the treeview receives focus it will automatically select the first node if none is selected. you can test this by clicking anywhere on the treeview not just the +.

I tried a few things to overcome this but couldn't find anything working yet. I'll post when I get something
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:henryreynolds
ID: 17098839
Hi THANK you again for your help, I will for now put a dummy node their that says the menu name.

Enjoy your day

Henry
0
 

Author Comment

by:henryreynolds
ID: 17098860
I am accpeting your answer now, if you maybe found something else will you let me know
on my email address , IF you want I will first assk a question again then you can anwer my question and receive the points
0
 
LVL 28

Expert Comment

by:2266180
ID: 17099066
no need to. you can keep this question open
0
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 17100314
well, the only thing I could think of is using a bool value to check if it's the first access or not:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Timer1: TTimer;
    procedure TreeView1Collapsing(Sender: TObject; Node: TTreeNode;
      var AllowCollapse: Boolean);
    procedure TreeView1Expanding(Sender: TObject; Node: TTreeNode;
      var AllowExpansion: Boolean);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
  private
    { Private declarations }
    isfirst,
    doexp,
    docol:boolean;
    procedure doit;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.TreeView1Collapsing(Sender: TObject; Node: TTreeNode;
  var AllowCollapse: Boolean);
begin
  if docol then AllowCollapse:=false;
end;

procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
  var AllowExpansion: Boolean);
begin
  if doexp then AllowExpansion:=false;
end;

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  doit;
end;

procedure tform1.doit;
begin
  if not isfirst then
  begin
    isfirst:=true;
    exit;
  end;
  if treeview1.Selected.HasChildren then
  begin
    if treeview1.Selected.Expanded then
    begin
      doexp:=true;
      treeview1.Selected.Collapse(true);
      doexp:=false;
    end                            else
    begin
      docol:=true;
      treeview1.Selected.Expand(true);
      docol:=false;
    end;
  end                               else
    showmessage('here execute whatever')
end;

end.
0
 

Author Comment

by:henryreynolds
ID: 17102220
Hi thanx for your help this is 100%%%

Enjoy your day......

Henry
0
 
LVL 28

Expert Comment

by:2266180
ID: 17103447
if it's satisfactory, don't forget to accept an answer ;)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

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