?
Solved

Tree View Creating simple Menu

Posted on 2006-07-13
9
Medium Priority
?
245 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
[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
  • 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
Technology Partners: 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!

 

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

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!

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

649 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