Solved

Treeview help needed

Posted on 2009-05-19
2
146 Views
Last Modified: 2012-05-07
Dear Experts,

I have received code from an expert that works just fine.

When an user create a new folder/file
the new created file/folder must be automaticly collapsed (and that works fine)
But the new created file/folder must also be selected (and the code doesn't do that)

Who can help me with this?

Greetings, Peter Kiers
procedure TMainForm.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);

var item: TTreeNode;

begin

  item := TTreeNode.Create(TreeView1.Items);

  item.Text := aText;

  item.ImageIndex := aIndex;

  item.SelectedIndex := aIndex;

  TreeView1.Items.AddNode(item, aParent, aText, nil, naAddChild);

  item.MakeVisible;

end;

(*----------------------------------------*)

procedure TMainForm.NewFolder1Click(Sender: TObject);

var aText: string;

  aParent: TTreeNode;

begin

  aParent := nil;

  if TreeView1.Selected <> nil then 

  begin

    if TreeView1.Selected.ImageIndex = 0 then

      aParent := TreeView1.Selected

  end;

  if InputQuery('Folder', 'Enter a folder name', aText) then

    AddItem(aText, 0, aParent);

end;

(*----------------------------------------*)

procedure TMainForm.NewItem1Click(Sender: TObject);

var aText: string;

begin

  if (TreeView1.Selected <> nil) and (Treeview1.Selected.ImageIndex = 0) then 

  begin

    if InputQuery('New Item', 'New Item Name:', aText) then

      AddItem(aText, 1, TreeView1.Selected);

  end else 

    ShowMessage('Select a folder in the treeview first.');

end;

(*----------------------------------------*)

Open in new window

0
Comment
Question by:peterkiers
2 Comments
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 24420304
first set TreeView1.HideSelection := False;
this will allways show the selection, even when the treeview is not focused
I sometimes set RowSelect := True as well
but this prevents editing.

and then you only need to add 1 line:




procedure TMainForm.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);

var item: TTreeNode;

begin

  item := TTreeNode.Create(TreeView1.Items);

  item.Text := aText;

  item.ImageIndex := aIndex;

  item.SelectedIndex := aIndex;

  TreeView1.Items.AddNode(item, aParent, aText, nil, naAddChild);

  item.MakeVisible;

  TreeView1.Selected := item;

end;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 24420494
Thanks. P.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

11 Experts available now in Live!

Get 1:1 Help Now