Solved

Help needed with treeview

Posted on 2009-05-19
6
259 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

631 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