Solved

Treeview question

Posted on 2009-05-18
8
180 Views
Last Modified: 2012-05-07
Dear experts,

I have received a code from an expert and it works just fine.
But with this code your not able to create a folder in a folder.

What do I have to change:

Peter Kiers
(*----------------------------------------*)
procedure TMainForm.NewFolder1Click(Sender: TObject);
var aText: string;
begin
  if InputQuery('New Folder', 'New Folder Name:', aText) then
    AddItem(aText, 0, nil);
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;
(*----------------------------------------*)
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);
end;

Open in new window

0
Comment
Question by:peterkiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24411817
procedure TMainForm.NewFolder1Click(Sender: TObject);
var aText: string;
  aParent: TTreeNode;
begin
  if InputQuery('New Folder', 'New Folder Name:', aText) then
  begin
    aParent := nil;
    if TreeView1.Selected <> nil then
      case MessageDlg('Do you want to create the folder below the currently selected one (no = create at toplevel, yes = create below selected)', mtConfirmation, [mbOk, mbNo, mbCancel], 0) of
        mrYes: aParent := TreeView1.Selected;
        mrNo: ; // aParent := nil;
        mrCancel: Exit;
      end;
    AddItem(aText, 0, aParent);
  end;
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24411881
Code doesn't work, and a don't want the qeustion to be ask
so no messagedlg.

Gtrx, PK
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24412181
sorry, didn't test in Delphi, just typed it in firefoxy
procedure TForm1.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);
end;
 
procedure TForm1.btnAddFileClick(Sender: TObject);
begin
  if (TreeView1.Selected <> nil) and (TreeView1.Selected.ImageIndex = 0) then
  begin
    AddItem(Edit1.Text, 1, TreeView1.Selected);
  end else
    ShowMessage('First select a folder');
end;
 
procedure TForm1.btnAddFolderClick(Sender: TObject);
var aText: string;
begin
  if InputQuery('Folder', 'Enter a folder name', aText) then
    AddItem(aText, 0, nil);
end;
 
procedure TForm1.btnAddSubFolderClick(Sender: TObject);
var aText: string;
begin
  if (TreeView1.Selected <> nil) and (TreeView1.Selected.ImageIndex = 0) then
  begin
    if InputQuery('New Sub Folder', 'New Sub Folder Name:', aText) then
      AddItem(aText, 0, TreeView1.Selected);
  end else 
    ShowMessage('You must select a folder first !');
end;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:peterkiers
ID: 24412271
You made for adding a subfolder in a folder a new button
But I want to use the same add folder button
and I know its possible.
I have send an example.

Greetings, Peter Kiers
Naamloos.jpg
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24412379
okay, both of them into 1 with no messagedlg (check is on selected folder)


procedure TForm1.btnAddFolderClick(Sender: TObject);
var aText: string;
  aParent: TTreeNode;
begin
  aParent := nil;
  if TreeView1.Selected <> nil then 
  begin
    if TreeView1.Selected.ImageIndex = 0 then // check for folder
      aParent := TreeView1.Selected
    //else 
    //begin
    //  ShowMessage('Select a folder first');
    //  Exit;
    //end;
    // uncomment previous 5 lines if a file is selected and you only want this to add on selected folders
  end;
  if InputQuery('Folder', 'Enter a folder name', aText) then
    AddItem(aText, 0, aParent);
end;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 24412451
Much better, only the new created subfolder will not automaticly collapse
you have to press the + sign to see the created subfolder.

0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 24412632
ugh ... okay

procedure TForm1.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;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24412663
Your the best, 500 comming your way.

PK.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

738 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