Solved

Treeview question

Posted on 2009-05-18
8
177 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

22 Experts available now in Live!

Get 1:1 Help Now