Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Treeview help needed

Posted on 2009-05-19
2
Medium Priority
?
153 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 38

Accepted Solution

by:
Geert Gruwez earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Loops Section Overview
Suggested Courses

916 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