Make sure a folder-node is selected in the treeview.

Peter Kiers
Peter Kiers used Ask the Experts™
on
Dear Experts,

I have an application with a Treeview on it and 2 buttons.
One button is for making a File-node in the treeview and
the other is for making a Folder-node in the treeview.
I have put the code of the 2 buttons in the code-section.
These procedures works perfect, but I want to add a
"ShowMessage('Select a folder first');" to make sure
for the two procedures that a folder-node is selected.
Because:
1. You can only make a folder-node in a folder-node
2. A file-node can only be made in a folder-node

Who can help me with this?

Greetings, Peter Kiers
procedure TForm1.FileBtnClick(Sender: TObject);
begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) and
     (NewItemFrm.ShowModal = mrOk ) then
     AddItem(NewItemFrm.Edit1.Text ,17,TreeView1.Selected);
end;
 
procedure TForm1.FolderBtnClick(Sender: TObject);
 begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) and
     (NewFolderFrm.ShowModal = mrOk ) then
          AddItem(NewFolderFrm.Edit2.Text ,15,TreeView1.Selected);
end;
 
The NewFolderFrm:
 
procedure TNewFolderFrm.FormShow(Sender: TObject);
begin
 Edit2.Text := '';
 ActiveControl := Edit2;
end;
(*---------------------------------------------------*)
procedure TNewFolderFrm.OKBtn2Click(Sender: TObject);
begin
  if Edit2.Text <> '' then
    ModalResult := mrOk
     else
     begin
     ModalResult := mrNone;
     ActiveControl := Edit2;
     MessageBox(0, 'You must enter a folder name.', 'Error', MB_OK + MB_ICONEXCLAMATION);
     end;
end;
(*---------------------------------------------------*)
 
The NewItemFrm:
 
procedure TNewItemFrm.FormShow(Sender: TObject);
begin
 Edit1.Text := '';
 ActiveControl := Edit1;
end;
(*---------------------------------------------------*)
procedure TNewItemFrm.OKBtn1Click(Sender: TObject);
begin
  if Edit1.Text <> '' then
    ModalResult := mrOk
     else
     begin
      ModalResult := mrNone;
      ActiveControl := Edit1;
    MessageBox(0, 'You must enter an item name.', 'Error', MB_OK + MB_ICONEXCLAMATION);
    end;
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Oracle dba
Top Expert 2009
Commented:
could you explain why you don't go with the generic input way ?
procedure TForm1.FileBtnClick(Sender: TObject);
begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) then
  begin
    if NewItemFrm.ShowModal = mrOk then
     AddItem(NewItemFrm.Edit1.Text ,17,TreeView1.Selected); 
  end else 
    ShowMessage('You must select a folder first');
end;
 
procedure TForm1.FolderBtnClick(Sender: TObject);
 begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) then
  begin
    if (NewFolderFrm.ShowModal = mrOk ) then
      AddItem(NewFolderFrm.Edit2.Text ,15,TreeView1.Selected);
  end else 
    ShowMessage('You must select a folder first');
end;

Open in new window

Peter KiersOperator

Author

Commented:
In my previous questions all the experts adviced me to make a static form
instead of an Input Query. The reason is hard to explain and a long story.
I thank you for your solution.
500p's comming your way...

Grtz, Peter Kiers
Peter KiersOperator

Author

Commented:
Geert your the best.
Geert GOracle dba
Top Expert 2009

Commented:
i'm trying to follow why you go this way.
a static form hmm
experts mostly give a start for your code, but this doesn't mean the end of the code

if you are a professional programmer, i would strongly advise to look at developmentguru's code
and the benefits they will have in the long term

it is a way of building a library for the many applications you may need to write in the future

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial