Como saber quais objetos tem parent igual ao objeto atual.
Como podemos determinar quais objetos (no caso : forms) tem a propriedade ´parent´ igual a um determinado objeto ( no caso : pagecontrol).
Estou usando assim....
procedure TFrmPrincipal.PageControlPrincipalClose(Sender: TObject;
var AllowClose: Boolean);
var
i, j: integer;
StateCDS: boolean;
begin
StateCDS := False;
with PageControlPrincipal do
begin
for i := 0 to Screen.FormCount - 1 do
with Screen.Forms[i] do
begin
if Parent <> nil then
if Parent = ActivePage then
for j := 0 to ComponentCount - 1 do
if (Components[j] is TClientDataSet) then
if (Components[j] as TClientDataSet).Active then
if (Components[j] as TClientDataSet).ChangeCount > 0 then
StateCDS := True;
end;
if StateCDS then
begin
if
Application.MessageBox(´Atenção! Existem Alterações pendentes que serão descartadas...´
+ #13 + 13 + ´Você tem mesmo certeza ?´,
´Cancelamento de Operações de dados...´, MB_YESNO + MB_ICONWARNING +
MB_DEFBUTTON1) = IDNO then
AllowClose := False
else
begin
ActivePage.Destroy;
SelectNextPage(false);
PageControlPrincipalChange(nil);
end;
end
else
begin
ActivePage.Destroy;
SelectNextPage(false);
PageControlPrincipalChange(nil);
end;
end;
end;
Apesar de funcional , queria substituir o ´for´ por alguma atitude mais rápida e orientada, mas getparent e etcs.. não estão funcionando.
Agradeço antecipadamente.
Estou usando assim....
procedure TFrmPrincipal.PageControlPrincipalClose(Sender: TObject;
var AllowClose: Boolean);
var
i, j: integer;
StateCDS: boolean;
begin
StateCDS := False;
with PageControlPrincipal do
begin
for i := 0 to Screen.FormCount - 1 do
with Screen.Forms[i] do
begin
if Parent <> nil then
if Parent = ActivePage then
for j := 0 to ComponentCount - 1 do
if (Components[j] is TClientDataSet) then
if (Components[j] as TClientDataSet).Active then
if (Components[j] as TClientDataSet).ChangeCount > 0 then
StateCDS := True;
end;
if StateCDS then
begin
if
Application.MessageBox(´Atenção! Existem Alterações pendentes que serão descartadas...´
+ #13 + 13 + ´Você tem mesmo certeza ?´,
´Cancelamento de Operações de dados...´, MB_YESNO + MB_ICONWARNING +
MB_DEFBUTTON1) = IDNO then
AllowClose := False
else
begin
ActivePage.Destroy;
SelectNextPage(false);
PageControlPrincipalChange(nil);
end;
end
else
begin
ActivePage.Destroy;
SelectNextPage(false);
PageControlPrincipalChange(nil);
end;
end;
end;
Apesar de funcional , queria substituir o ´for´ por alguma atitude mais rápida e orientada, mas getparent e etcs.. não estão funcionando.
Agradeço antecipadamente.
Emerson Azevedo
Curtidas 0
Respostas
Emerson Azevedo
18/03/2008
sobe
GOSTEI 0
Emerson Nascimento
18/03/2008
não conheço melhor forma que o uso do [i:a6602e6c22]for[/i:a6602e6c22], porém eu fiz algumas alterações de modo a sair dele assim que a variável tiver conteúdo verdadeiro. talvez isso melhore a performance.
procedure TFrmPrincipal.PageControlPrincipalClose(Sender: TObject;
var AllowClose: Boolean);
var
i, j: integer;
StateCDS: boolean;
begin
StateCDS := False;
with PageControlPrincipal do
begin
// faz a verredura nos forms abertos
for i := 0 to Screen.FormCount - 1 do
with Screen.Forms[i] do
begin
if (Parent <> nil) and (Parent = ActivePage) then
// caso o form seja ´filho´ da página ativa no pagecontrol,
// faz a verredura nos componentes do form
for j := 0 to ComponentCount - 1 do
if (Components[j] is TClientDataSet) then
if (Components[j] as TClientDataSet).Active and
((Components[j] as TClientDataSet).ChangeCount > 0) then
begin
StateCDS := True;
break; // se já encontrou um dataset aberto,
// não é mais necessário varrer os COMPONENTES
end;
// se já encontrou um dataset aberto,
// não é mais necessário varrer os FORMS
if StateCDS then
break;
end;
AllowClose := not StateCDS or (StateCDS and
(Application.MessageBox(´Atenção! Existem Alterações pendentes que serão descartadas...´
+ 13 + 13 + ´Você tem mesmo certeza ?´,
´Cancelamento de Operações de dados...´, MB_YESNO + MB_ICONWARNING +
MB_DEFBUTTON1) = IDNO));
// a flag AllowClose=True fecha a página automaticamente.
// de qualquer forma, se você mesmo quiser controlar
// o fechamento, habilite o código abaixo
{ if AllowClose then
begin
ActivePage.Destroy;
SelectNextPage(false);
PageControlPrincipalChange(nil);
end;}
end;
end;GOSTEI 0