• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Tree View Creating simple Menu

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
henryreynolds
Asked:
henryreynolds
  • 5
  • 4
1 Solution
 
2266180Commented:
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
 
henryreynoldsAuthor Commented:
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
 
2266180Commented:
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
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.

 
henryreynoldsAuthor Commented:
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
 
henryreynoldsAuthor Commented:
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
 
2266180Commented:
no need to. you can keep this question open
0
 
2266180Commented:
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
 
henryreynoldsAuthor Commented:
Hi thanx for your help this is 100%%%

Enjoy your day......

Henry
0
 
2266180Commented:
if it's satisfactory, don't forget to accept an answer ;)
0

Featured Post

Industry Leaders: 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!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now