[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1306
  • Last Modified:

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
0
DigitalNam
Asked:
DigitalNam
  • 4
  • 4
1 Solution
 
epasquierCommented:
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
 
epasquierCommented:
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
DigitalNamAuthor Commented:
:-) That sorted the problem. Thank you very much. Only one other thing, the files don't execute.
0
 
epasquierCommented:
> 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
 
epasquierCommented:
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
 
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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now