Solved

Treeview question

Posted on 2009-05-18
8
175 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
  • 4
  • 4
8 Comments
 
LVL 36

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 36

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 36

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 36

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

13 Experts available now in Live!

Get 1:1 Help Now