Solved

Treeview help needed

Posted on 2009-05-19
2
148 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 37

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2 61
select query - oracle 16 100
Error E2158 compiling with Delphi XE10 Seattle 2 102
How to build JSON File in Delphi 6 3 36
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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