Fórum Edit dinâmico - Delphi #613713
06/01/2021
0
Quero criar uns componentes dinamicamente igual ao aplicativo dos contatos da "apple" e "Microsoft". Quando o usuário clica em "incluir telefone" aparecerá um novo "edit" para ele preencher os dados. Se o usuário decidir desistir de incluir o contato ele pode clicar no botãozinho à direita do edit e excluir o próprio edit criado. Então resolvi criar um "TButtonEdit" dinamicamente mas preciso programar o evento "OnRightButtonClick := FreeAndNil(MeuEdit);". Fiz o código abaixo, mas está dando erro de comilação: [dcc64 Error] uFrameContactos.pas(101): E2010 Incompatible types: 'TNotifyEvent' and 'procedure, untyped pointer or untyped parameter'
Alguém pode dar uma força?
CÓDIGO:
procedure TFrameContactos.CriarEditDinamicamente;
var MeuEdit: TButtonedEdit;// EditNome : string;
begin
MeuEdit := TButtonedEdit.Create(Self);
with MeuEdit do
begin
Parent := Self.pnlTelefono;
Images := frmPrincipal.ImgListButtons;
RightButton.ImageIndex:= 30;
Left := 179;
Height := 25;
Top := 88;
Width := 460;
Align := alTop;
MeuEdit.SetFocus;
MeuEdit.SelectAll;
OnRightButtonClick := FreeAndNil(MeuEdit);
end;
end;
TENTATIVAS REALIZADAS:
Já use o destrói mas acontece erro também;
já tentei dar nome ao Edit, mas se o usuário quiser criar outro edit o software acusa que já exist edit com aquele nome e impede a criação dois edits ou mais;
Já consegui pessoal, usando a própria ajuda do Delphi aqui. Para destruir o objeto recém criado basta fazer isso:
MeuEdit.OnRightButtonClick := EditRightButtonClick;
procedure TFrameContactos.EditRightButtonClick(Sender : TObject);
begin
Sender.Destroy;
end;
MINHA PRÓXIMA DÚVIDA É A SEGUINTE:
Imaginem que o usuário resolveu incluir 3 telefones, e agora ele resolve salvar cada um desses dados no banco. Como faço para pegar os valores que estão nos edits e guardar cada um deles no banco? Pois os edits criados dinamicamente não têm nome conhecido. Como fazer? Preciso identificar quando os telefones são "celular, fixo, Zap" etc.
Wilton Santos
Curtir tópico
+ 0Post mais votado
07/01/2021
+----------+-----------------------------------+----------+ | tipo | contato | whatsapp | +----------+-----------------------------------+----------+ | celular | 11 98665-4321 | x | | fixo | 11 2345-6789 | o | | email | teste@dominio.com.br | | | email | maisumteste@dominio.com.br | | | | | | | | | | +----------+-----------------------------------+----------+
mas se quer mesmo criar edits...
isto somente será necessário se você usar algum TButtonedEdit para outras finalidades dentro de Self.pnlTelefono:
procedure TFrameContactos.CriarEditDinamicamente;
var
numSequencia: integer;
strEditContato: string;
begin
// inicializa o 'randomizador'
Randomize;
// procura um nome disponível
repeat
numSequencia := Random(1000); // permite criar até 1000 edits distintos (0 a 999)
strEditContato := 'editContato_'+FormatFloat('000',numSequencia);
until (Self.pnlTelefono.FindComponent(strEditContato) = nil)
// cria o componente
with TButtonedEdit.Create(Self.pnlTelefono) do
begin
Name := strEditContato; // atribui o nome obtido
Parent := Self.pnlTelefono;
Images := frmPrincipal.ImgListButtons;
RightButton.ImageIndex:= 30;
Left := 179;
Height := 25;
Top := 88;
Width := 460;
Align := alTop;
OnRightButtonClick := EditRightButtonClick;
SetFocus;
SelectAll;
end;
end;// gravando telefones
for i := 0 to Self.pnlTelefono.ComponentCount-1 do
if (Self.pnlTelefono.Components[i] is TButtonedEdit) and (LeftStr(Self.pnlTelefono.Components[i].Name) = 'editContato_') then
[aqui a gravação do telefone]se houver TButtonedEdit dentro de Self.pnlTelefono somente para edição do contato:
procedure TFrameContactos.CriarEditDinamicamente;
begin
// cria o componente
with TButtonedEdit.Create(Self.pnlTelefono) do
begin
Parent := Self.pnlTelefono;
Images := frmPrincipal.ImgListButtons;
RightButton.ImageIndex:= 30;
Left := 179;
Height := 25;
Top := 88;
Width := 460;
Align := alTop;
OnRightButtonClick := EditRightButtonClick;
SetFocus;
SelectAll;
end;
end;// gravando telefones
for i := 0 to Self.pnlTelefono.ComponentCount-1 do
if (Self.pnlTelefono.Components[i] is TButtonedEdit) then
[aqui a gravação do telefone]Emerson Nascimento
Gostei + 1
Mais Posts
07/01/2021
Emerson Nascimento
daí, quando for gravar, basta varrer o Self.pnlTelefono e pegar os componentes pertinentes, observando o prefixo do nome:
// gravando telefones
for i := 0 to Self.pnlTelefono.ComponentCount-1 do
if (Self.pnlTelefono.Components[i] is TButtonedEdit) and (LeftStr(Self.pnlTelefono.Components[i].Name,12) = 'editContato_') then
[aqui a gravação do telefone]Gostei + 0
07/01/2021
Wilton Santos
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)