Criar evento em tempo de execução
Galera, quero agradecer a todos pela a ajuda que tenho recebido!
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.
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
Curtidas 0
Respostas
Paulo_amorim
13/01/2005
Olá
Crie a sua rotina com os mesmos parâmetros do evento que vc quer criar para o Menu
Depois faça
Espero que ajude
Até+
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
Debug
13/01/2005
Fiz o seguinte:
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
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
Piaum3
13/01/2005
sobe
GOSTEI 0
Massuda
13/01/2005
Mias esta dando esse erro:
Incompatibli types: TNotifyEvent and procedure, untyped pointer or untyped parameter
Isso está acontecendo porque AbrirUrl não tem assinatura compatível com TNotifyEvent.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
Debug
13/01/2005
Massuda, muito obrigado.....
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
Valeu mesmo.....
GOSTEI 0
Marcelo_mileris
13/01/2005
Dica tirada do DTDelphi 2.6
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....
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