Popup menu with listbox

Think this is an easy question. I have a button which opens the Opendialog so the user can choose an executable file. This file is added to a listbox. Now the question is, how do I populate a popup menu with the items in the listbox and when the user clicks on the newly created item in the menu to execute that program.

Many thanks
LVL 1
DigitalNamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Emmanuel PASQUIERFreelance Project ManagerCommented:
for i:=0 to listBox.Items.Count-1 do With popmenu.Items.Add do
  begin
   Caption:=listBox.Items[i];
   OnClick:=MenuClick;
  end;


procedure TForm1.MenuClick(Sender:TObject);
begin
 With Sender As TMenuItem do
  ShellExecute(Handle,'open',PChar(Caption),nil,nil,SW_SHOWNORMAL);
end;
0
DigitalNamAuthor Commented:
Thanks for the quick reply. I get the following error. There is no overloaded version of 'Add' that can be called with these arguments. I am using Delphi 7.
0
Emmanuel PASQUIERFreelance Project ManagerCommented:
damn right, I've been too quick. I tested this now :
Var
 i:integer;
 item:TMenuItem;
begin
 popmenu.Items.Clear;
 For i:=0 to listBox.Items.Count-1 do
  begin
   item:=TMenuItem.Create(popmenu);
   With item do
    begin
     Caption:=listBox.Items[i];
     OnClick:=MenuClick;
    end;
   popmenu.Items.Add(item);
  end;
end;

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

DigitalNamAuthor Commented:
:-) That sorted the problem. Thank you very much. Only one other thing, the files don't execute.
0
Emmanuel PASQUIERFreelance Project ManagerCommented:
> the files don't execute.

how undisciplined these executables can be :-)

here is the reference for ShellExecute :
http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx

put a breakpoint on this ShellExecute line and check that
a) the event is fired when you click the menu
b) the Caption value holds a full qualified path to your executable, and has not been tampered because we used the menu Caption that usually serves other purpose. It's possible the menu manager added some & to add an automatic shortcut.

That would be better to find a way to hold the file path elsewhere, leaving you the possibility to display what you want, and not having such probable problems. I'll see that
0
Emmanuel PASQUIERFreelance Project ManagerCommented:
yep, TMenuItem properties like AutoHotKeys or AutoLineReduction could tamper the caption.

Let's put the file path in Hint property, that would be better
Var
 i:integer;
 item:TMenuItem;
begin
 popmenu.Items.Clear;
 For i:=0 to listBox.Items.Count-1 do
  begin
   item:=TMenuItem.Create(popmenu);
   With item do
    begin
     Hint:=listBox.Items[i];
     Caption:=ExtractFileName(Hint);
     OnClick:=MenuClick;
    end;
   popmenu.Items.Add(item);
  end;
end;

procedure TForm1.MenuClick(Sender:TObject);
begin
 With Sender As TMenuItem do
  ShellExecute(Handle,'open',PChar(Hint),nil,nil,SW_SHOWNORMAL);
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DigitalNamAuthor Commented:
Yes it puts in an automatic shortcut, but otherwise it works. Will see if I can figure out how to remove the shortcut. Thank you very much for the help.
0
DigitalNamAuthor Commented:
Very nice, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.