Link to home
Start Free TrialLog in
Avatar of LeTay
LeTay

asked on

React on item selected in TTreeView component

I have created a TTreeView component on a form
Now, when the user 'navigates' through this structure, I want to be able to do some work when individual 'items' are highlighted.
One way is to code the event OnClick, but I am surprised that there is no such kind of event OnSelected that I can use when, for example, the user simply uses the down and up arrows to navigate.
Is there such event or an equivalent trick ?
SOLUTION
Avatar of mottor
mottor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DavidBirch2dotCom
DavidBirch2dotCom

Which component are you using ? the Delphi one or the Virtual Treeview from www.delphi-gems.com ? (I prefer the later) but if its the normal delphi one, then

try

procedure TForm1.TreeView1Click(Sender: TObject);
Var
        Mynode: TTreeNode;
begin
   Mynode:= TreeView1.Selected;
end;

to get hold of the node when the user clicks on the treeview, or if you want get the node that has been changed/edited then use the ONchange event, which gives the node that has been changed (see below).  However that wont be fired if the user simply clicks the tree.

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

end;

If you need to get news of the user changing the node they have selected, such as by using the arrow keys or sometihng then its probably easiest to dump a TTimer on the form set the interval for about a quater of a second (250) then have the following code

{$R *.dfm}

Var
        LastSelected: TTreeNode;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
   If (LastSelected <> TreeView1.Selected) then
   Begin
      Lastselected:= TreeView1.Selected;
      HELPanotherNodeHasBeenSelected(LastSelected);
   end;
end;

procedure TForm1.HELPanotherNodeHasBeenSelected(node: TTreeNode);
Begin
// what you going to do about it goes here ;-)
end;

hope that helps, If you need clarifaction then just ask

David
Avatar of LeTay

ASKER

That OnChange is the answer.
I coded my little stuff there (do some work...) but now the selected item is not highlighted anymore.
I suppose I have to highlight it myself, but how ?
SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
set HideSelection to False;
Avatar of LeTay

ASKER

Indeed the code I executed is showing another form with details of the selected item.
So the primary form containing the TTreeView "looses" the focus.
I just add to give it the focus back -> Form1.SetFocus and now it works