Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help needed with treeview

Posted on 2009-05-19
6
Medium Priority
?
265 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 38

Assisted Solution

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

877 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