Solved

Help needed with treeview

Posted on 2009-05-19
6
258 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
Industry Leaders: 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
LAN or WAN ? 11 121
Multiple image collision 13 111
Delphi: ForceDirectory plain function 7 88
Delphi android app hide keyboard 3 84
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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