GARANTIR DESCONTO

Fórum Como saber quais objetos tem parent igual ao objeto atual. #355612

18/03/2008

0

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.


Emerson Azevedo

Emerson Azevedo

Responder

Posts

19/03/2008

Emerson Azevedo

sobe


Responder

Gostei + 0

20/03/2008

Emerson Nascimento

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;



Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar