Solved

Help needed with treeview

Posted on 2009-05-19
6
252 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

10 Experts available now in Live!

Get 1:1 Help Now