Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

runtime popup

Status
Not open for further replies.

filipe26

Programmer
Mar 17, 2003
152
PT
hi im trying to create a popup menu on runtime.I have the following code that creates the menu items.

for x:=0 to opcoes.Memo2.Lines.Count do
begin
combobox1.Items.Add(opcoes.Memo2.lines.Strings[x]);
NewItem.Caption :=opcoes.Memo2.lines.Strings[x];
pop.Items.Add(NewItem);
newitem.OnClick:=newitemclick;
end;

so far just fine.

On the event onclick that i created newitemclick is:

procedure TNavegain.newitemClick(sender:Tobject);
begin
dm.ComprasinQ.Close;
dm.ComprasinQ.SQL.Clear;
dm.ComprasinQ.SQL.Add('select * from invernomarfel');
dm.ComprasinQ.SQL.Add('where tipo=:p1');
dm.ComprasinQ.Params.ParamByName('p1').Value:=newitem.Caption;
dm.ComprasinQ.Open;
end;

the problem is that in newitem.caption i have always '' and not the name of the menu.

Thanks.
 
Hey filipe.

I'll start from the second part - your onClick handler.
Code:
procedure TNavegain.newitemClick(sender:Tobject);
begin
  dm.ComprasinQ.Close;
  dm.ComprasinQ.SQL.Clear;
  dm.ComprasinQ.SQL.Add('select * from invernomarfel');
  dm.ComprasinQ.SQL.Add('where tipo=:p1');
{1}dm.ComprasinQ.Params.ParamByName('p1').Value := newitem.Caption; 
//in line {1} you refer not to a clicked item's caption,
//but to the caption of some global var as far as I can 
//understand, thus it will be the same string always;

// how it's supposed to be:
// if (Sender is TMenuItem)then
// dm.ComprasinQ.Params.ParamByName('p1').Value :=  
// TMenuItem(Sender).Caption;
  dm.ComprasinQ.Open;
end;

Now as for creating menu items. What you do is creating items that all refer to the same object (newitem) and you are setting the caption of only one object, so I guess the last line in your Memo is <CR><LF> which gives you the empty caption for all elements in popup menu.

Try the following example:
Code:
var
  ANewItem : TMenuItem;
...
begin
...
  for x:=0 to opcoes.Memo2.Lines.Count - 1 do
  begin
    combobox1.Items.Add(opcoes.Memo2.lines.Strings[x]);
    ANewItem := TMenuItem.Create(nil);
    ANewItem.OnClick := newitemclick;
    ANewItem.Caption := opcoes.Memo2.lines.Strings[x];
    pop.Items.Add(ANewItem);
  end;
...
end;
And one more thing: when rebiulding your popup menu, don't forget to Free items that you had created before.

Cheers.

--- McMerfy

 
Yes,it works,thanks but in the caption always has a & before the item name why?
 
filipe,

The & character shows that the next character is used as a hot key (i.e. shortcut key) for that menu item.
Set the AutoHotKeys property of your popup menu to maManual to prevent Delphi from automatically setting the hot keys in your menu.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top