?
Solved

Treeview question

Posted on 2009-05-18
8
Medium Priority
?
183 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 38

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 38

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 38

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 38

Accepted Solution

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

800 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