Solved

Tree View Creating simple Menu

Posted on 2006-07-13
9
216 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:ciuly
Comment Utility
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
Comment Utility
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:ciuly
Comment Utility
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
 

Author Comment

by:henryreynolds
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:henryreynolds
Comment Utility
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:ciuly
Comment Utility
no need to. you can keep this question open
0
 
LVL 28

Accepted Solution

by:
ciuly earned 500 total points
Comment Utility
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
Comment Utility
Hi thanx for your help this is 100%%%

Enjoy your day......

Henry
0
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
if it's satisfactory, don't forget to accept an answer ;)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now