Link to home
Start Free TrialLog in
Avatar of DigitalNam
DigitalNamFlag for Namibia

asked on

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
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

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;
Avatar of DigitalNam

ASKER

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

:-) That sorted the problem. Thank you very much. Only one other thing, the files don't execute.
> 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
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Very nice, thanks.