Fórum Criar evento em tempo de execução #265184
13/01/2005
0
tenho uma procedimento que cria em tempo em execução menus.
var NovoItem : TMenuItem;
begin
ApagaMenu;
QUrl.Close;
QUrl.Open;
QUrl.First;
while not QUrl.Eof do
begin
NovoItem := TMenuItem.Create(Self);
NovoItem.Caption := QUrlURL.AsString;
PmUrl.Items.Add(NovoItem);
QUrl.Next;
end;
Oque eu não estou conseguindo e não tenho nem ideia de como colocar eventos para cada menu que for criado.
Alguem poderia me dar um help.
Desde já muito obrigado.
Debug
Curtir tópico
+ 0Posts
13/01/2005
Paulo_amorim
Crie a sua rotina com os mesmos parâmetros do evento que vc quer criar para o Menu
Depois faça
NovoItem.<seu evento> := <sua rotina>;
Espero que ajude
Até+
Gostei + 0
13/01/2005
Debug
procedure TBandForm.PmUrlPopup(Sender: TObject);
var NovoItem : TMenuItem;
begin
ApagaMenu;
QUrl.Close;
QUrl.Open;
QUrl.First;
while not QUrl.Eof do
begin
NovoItem := TMenuItem.Create(Self);
NovoItem.Caption := QUrlURL.AsString;
NovoItem.OnClick(Self);
NovoItem.OnClick := AbrirUrl(NovoItem.Caption);
PmUrl.Items.Add(NovoItem);
QUrl.Next;
end;
end;
Mias esta dando esse erro:
Incompatibli types: TNotifyEvent and procedure, untyped pointer or untyped parameter
Gostei + 0
13/01/2005
Piaum3
Gostei + 0
13/01/2005
Massuda
Pelo seu código, imagino que AbrirUrl seja declarada assim:
procedure TBandForm.AbrirUrl(Url: string)
procedure AbrirUrl(Url: string)
Uma possível implementação seria:
procedure TBandForm.PmUrlPopup(Sender: TObject); var NovoItem : TMenuItem; begin ApagaMenu; QUrl.Close; QUrl.Open; QUrl.First; while not QUrl.Eof do begin NovoItem := TMenuItem.Create(Self); NovoItem.Caption := QUrlURL.AsString; NovoItem.OnClick := DoOnUrlClick; PmUrl.Items.Add(NovoItem); QUrl.Next; end; end; // Incluir a declaração deste método na parte privada da classe TBandForm procedure TBandForm.DoOnUrlClick(Sender: TObject); var Item : TMenuItem; begin Item := Sender as TMenuItem; AbrirUrl(Item.Caption); end;
Gostei + 0
13/01/2005
Debug
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
Gostei + 0
13/01/2005
Marcelo_mileris
Você pode criar qualquer componente do delphi de forma dinamica, incluindo todos os componentes visuais(buttons, textedits, maskedits, labels)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
contador:integer;
implementation
{$R *.DFM}
uses stdctrls;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var b:tbutton;
begin
b:=Tbutton.create(self);
b.visible:=false;
b.parent:=self;
b.left:=x;
b.top:=y;
b.name:=´Btn´+inttostr(contador);
b.Caption:=´Clique-me´;
inc(contador);
b.visible:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
contador:=1;
end;
end.
Se quiser associar um evento ao botao é só copiar os procedimento clicou e alterar
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var b:tbutton;
begin
b:=Tbutton.create(self);
b.visible:=false;
b.parent:=self;
b.left:=x;
b.top:=y;
b.name:=´Btn´+inttostr(contador);
b.Caption:=´Clique-me´;
inc(contador);
b.visible:=true;
b.onclick:=clicou;
end;
procedure TForm1.clicou(sender: TObject);
begin
ShowMessage(´Clicou!!!´);
end;
Com isto concluimos que podemos criar qualquer coisa em tempo de execução com economia de memória. Basta para isso dar o create da classe....
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)