Solved

Help needed with treeview

Posted on 2009-05-19
6
254 Views
Last Modified: 2012-05-07
Dear Experts,

I want the user to put a name in the inputquery when the user want to create a new file/folder.
Making a treeview node without a name is not aloud. The system than must reject when the user
press the OK-button and the editbox is empty. And showing this message:

You must enter an item name

or

You must enter a folder name¨

Is this possible

Peter Kiers
(*----------------------------------------*)

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
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
bokist earned 250 total points
ID: 24420747
one of possible solutions :

procedure TMainForm.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
var item: TTreeNode;
begin
  if length(trim(aText)) = 0 then
     begin
     showmessage('Can't add item without text!!');
     Exit;
  end;
  item := TTreeNode.Create(TreeView1.Items);
  item.Text := aText;
  item.ImageIndex := aIndex;
  item.SelectedIndex := aIndex;
  TreeView1.Items.AddNode(item, aParent, aText, nil, naAddChild);
 end;
0
 
LVL 6

Expert Comment

by:bokist
ID: 24421015
another solution :

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
     begin
     if length(trim(aText)) = 0 then
        begin
        showmessage('You must enter a folder name!!');
        Exit;
     end;
     AddItem(aText, 0, aParent);
   end;
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
       begin
       if length(trim(aText)) = 0 then
          begin
          showmessage('You must enter an item name!!');
          Exit;
       end;
       AddItem(aText, 1, TreeView1.Selected);
     end;
  end else
    ShowMessage('Select a folder in the treeview first.');
end;
(*----------------------------------------*)
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24421101
I like the solution, only the inputquery has to stay
when a user select the button to make a new folder or file
an inputquery appears, when the user clicks on OK
and their is nothing filled in the editbox an message appears
but the inputquery must still be their.

peter
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 6

Expert Comment

by:bokist
ID: 24421358
check this

  aText := '';
  if (TreeView1.Selected <> nil) and (Treeview1.Selected.ImageIndex = 0) then
     begin
     while length(trim(aText)) = 0  do
        if not (InputQuery('Folder', 'Enter a folder name', aText)) then Break;
  end else
     ShowMessage('Select a folder in the treeview first.');
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 total points
ID: 24421378
provide a new function instead of the InputQuery for validating ...

using below function:

procedure TMainForm.NewItem1Click(Sender: TObject);
var aText: string;
begin
  if (TreeView1.Selected <> nil) and (Treeview1.Selected.ImageIndex = 0) then
  begin
    if RequestName('New Item', 'New Item Name:', aText, 'You must enter an item name') then
      AddItem(aText, 1, TreeView1.Selected);
  end else
    ShowMessage('Select a folder in the treeview first.');
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 RequestName('Folder', 'Enter a folder name', aText) then
    AddItem(aText, 0, aParent);
end;


function TMainForm.RequestName(aTitle, aCaption: string; var aValue: string; aErrMessage: string = 'You must enter a folder name'): boolean;

var Done: Boolean;

  Temp: string;

begin

  Temp := aValue;

  Result := False;

  repeat

    Done := False;

    if InputQuery(aTitle, aCaption, Temp) then 

    begin

      if Trim(Temp) <> '' then 

      begin

        Result := True;

        aValue := Trim(Temp);

      end else

        ShowMessage(aErrMessage);

    end else Done := True;

  until Done or Result;

end;

      

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 24421870
Thanks.
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

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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

24 Experts available now in Live!

Get 1:1 Help Now