Close a form is closed all

Asked

Viewed 57 times

1

I have 2 Forms in a Delphi application. When I open the system to register I open the two Forms, the main and the registration. The problem comes when I try to close the registration form because it closes the whole system and I do not know what to do... I will leave down the codes.

  • Register form layer.

    procedure TfrmPrincipal.btnClienteClick(Sender: TObject);
    begin
    if frmCliente = nil then
      Application.CreateForm(TfrmCliente, frmCliente);
    
    frmCliente.Parent := frmPrincipal;
    frmCliente.Show;
    
    frmPrincipal.pnTelaPrincipal_Titulo1.Enabled := False;
    frmPrincipal.pnNavegacao.Enabled             := False;
    frmPrincipal.pnBaseManutencao01.Enabled      := False;
    end;
    
  • Form layer Close frmCliente.

    procedure TfrmCliente.btnCloseClick(Sender: TObject);
    begin
      frmPrincipal.pnTelaPrincipal_Titulo1.Enabled  := True;
      frmPrincipal.pnNavegacao.Enabled        := True;
      frmPrincipal.pnBaseManutencao01.Enabled := True;
    
      frmCliente.Close;
    
      frmPrincipal.AtualizaBotoesAdicionais;
    end;
    

After Breackpoint goes through end; of btnCloseClick it goes to the Formclose of the frmPrincipal closed the whole system.

procedure TfrmPrincipal.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  DM.qryUsoGeral.Close;
  DM.qryLocaliza.Close;

  FreeAndNil(TfrmPrincipal);
end;

I want to know why that is and how to fix it.

On request I’ll leave the . dpr here

program Autosad;

{$R *.dres}

uses
  Windows,
  Vcl.Forms,
  UHPI in 'FONTES\Classe\UHPI.pas',
  UAcompanhamentoProcessos in 'FONTES\UAcompanhamentoProcessos.pas' {frmAcompanhamentoProcessos},
  UAlterarSenha in 'FONTES\UAlterarSenha.pas' {frmAlterarSenha},
  UAndamentoProcessual in 'FONTES\UAndamentoProcessual.pas' {frmAndamentoProcessual},
  UAtualizacoesVersao in 'FONTES\UAtualizacoesVersao.pas' {frmAtualizacoesVersao},
  UCentroCustos in 'FONTES\UCentroCustos.pas' {frmCentroCusto},
  UClienteAdicionais in 'FONTES\UClienteAdicionais.pas' {frmClienteAdicionais},
  UConsultas in 'FONTES\UConsultas.pas' {frmConsultas},
  UDataModuleGeral in 'FONTES\UDataModuleGeral.pas' {DataModuleGeral: TDataModule},
  UDataModuleGeral2 in 'FONTES\UDataModuleGeral2.pas' {DataModuleGeral2: TDataModule},
  UDataModuleGeral3 in 'FONTES\UDataModuleGeral3.pas' {DataModuleGeral3: TDataModule},
  UDataModuleRelatorios in 'FONTES\UDataModuleRelatorios.pas' {DataModuleRelatorios: TDataModule},
  UEmprestimos in 'FONTES\UEmprestimos.pas' {frmEmprestimos},
  UEntradaSistema in 'FONTES\UEntradaSistema.pas' {frmEntradaSistema},
  UEnviaEmailClientes in 'FONTES\UEnviaEmailClientes.pas' {frmEnviaEmailClientes},
  UFiltrosFluxoCaixa in 'FONTES\UFiltrosFluxoCaixa.pas' {frmFiltrosFluxoCaixa},
  UFiltrosGraficos in 'FONTES\UFiltrosGraficos.pas' {frmFiltrosGraficos},
  UFinanceiro in 'FONTES\UFinanceiro.pas' {frmFinanceiro},
  UFluxoCaixa in 'FONTES\UFluxoCaixa.pas' {frmFluxoCaixa},
  UGraficosEstatisticos in 'FONTES\UGraficosEstatisticos.pas' {frmGraficosEstatisticos},
  UInfoCliente in 'FONTES\UInfoCliente.pas' {frmInfoCliente},
  UManutencaoAdvogados in 'FONTES\UManutencaoAdvogados.pas' {frmManutencaoAdvogados},
  UManutencaoAgenda in 'FONTES\UManutencaoAgenda.pas' {frmManutencaoAgenda},
  UManutencaoAudiencias in 'FONTES\UManutencaoAudiencias.pas' {frmManutencaoAudiencias},
  UManutencaoBiblioteca in 'FONTES\UManutencaoBiblioteca.pas' {frmManutencaoBiblioteca},
  UManutencaoClientes in 'FONTES\UManutencaoClientes.pas' {frmManutencaoClientes},
  UManutencaoContasPagar in 'FONTES\UManutencaoContasPagar.pas' {frmManutencaoContasPagar},
  UManutencaoContatos in 'FONTES\UManutencaoContatos.pas' {frmManutencaoContatos},
  UManutencaoContratos in 'FONTES\UManutencaoContratos.pas' {frmManutencaoContratos},
  UManutencaoFornecedores in 'FONTES\UManutencaoFornecedores.pas' {frmManutencaoFornecedores},
  UManutencaoHonorarios in 'FONTES\UManutencaoHonorarios.pas' {frmManutencaoHonorarios},
  UManutencaoParametros in 'FONTES\UManutencaoParametros.pas' {frmManutencaoParametros},
  UManutencaoProcessos in 'FONTES\UManutencaoProcessos.pas' {frmManutencaoProcessos},  // <== Estou Utilizando esses From para fazer os testes 
  UManutencaoUnidades in 'FONTES\UManutencaoUnidades.pas' {frmManutencaoUnidades},
  UManutencaoUsuarios in 'FONTES\UManutencaoUsuarios.pas' {frmManutencaoUsuarios},
  UMudancaGrupalProcessos in 'FONTES\UMudancaGrupalProcessos.pas' {frmMudancaGrupalProcessos},
  UNovoCEP in 'FONTES\UNovoCEP.pas' {frmNovoCEP},
  UNovoFornecedor in 'FONTES\UNovoFornecedor.pas' {frmNovoFornecedor},
  UNovoModelo in 'FONTES\UNovoModelo.pas' {frmNovoModelo},
  UParteContrariaAdicionais in 'FONTES\UParteContrariaAdicionais.pas' {frmParteContrariaAdicionais},
  UPesquisaClientes in 'FONTES\UPesquisaClientes.pas' {frmPesquisaClientes},
  UPesquisaContasPagar in 'FONTES\UPesquisaContasPagar.pas' {frmPesquisaContasPagar},
  UPesquisaContatos in 'FONTES\UPesquisaContatos.pas' {frmPesquisaContatos},
  UPesquisaContratos in 'FONTES\UPesquisaContratos.pas' {frmPesquisaContratos},
  UPesquisaFornecedores in 'FONTES\UPesquisaFornecedores.pas' {frmPesquisaFornecedores},
  UPesquisaHonorarios in 'FONTES\UPesquisaHonorarios.pas' {frmPesquisaHonorarios},
  UPesquisaProcessos in 'FONTES\UPesquisaProcessos.pas' {frmPesquisaProcessos},
  UPublicacoes in 'FONTES\UPublicacoes.pas' {frmPublicacoes},
  USelecaoRelatoriosContasPagar in 'FONTES\USelecaoRelatoriosContasPagar.pas' {frmSelecaoRelatoriosContasPagar},
  USelecaoRelatoriosHonorarios in 'FONTES\USelecaoRelatoriosHonorarios.pas' {frmSelecaoRelatoriosHonorarios},
  USelecaoRelatoriosProcessos in 'FONTES\USelecaoRelatoriosProcessos.pas' {frmSelecaoRelatoriosProcessos},
  USplash in 'FONTES\USplash.pas' {frmSplash},
  UTelaPrincipal in 'FONTES\UTelaPrincipal.pas' {frmTelaPrincipal},
  UUtilitarios in 'FONTES\UUtilitarios.pas' {frmUtilitarios},
  UVisualizadorDeProcessos in 'FONTES\UVisualizadorDeProcessos.pas' {frmVisualizadorDeProcessos},
  UNovasPublicacoes in 'FONTES\UNovasPublicacoes.pas' {frmNovasPublicacoes},
  USobre in 'FONTES\USobre.pas' {frmSobre},
  Vcl.Themes,
  Vcl.Styles,
  UFuncoes in 'Fontes\Classe\UFuncoes.pas';

{$R *.res}
{$SetPEFlags IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP}
{$DEFINE IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = $ 8000}
{$SetPEOptFlags IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE}
{$SetPEFlags IMAGE_FILE_RELOCS_STRIPPED or IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP or IMAGE_FILE_NET_RUN_FROM_SWAP}

begin
  {$IFDEF DEBUG}
  ReportMemoryLeaksOnShutdown := True;
  {$ENDIF}
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title             := 'Autosad - Gerenciador de Processos Jurídicos';
  Application.CreateForm(TDataModuleGeral, DataModuleGeral);
  Application.CreateForm(TDataModuleGeral2, DataModuleGeral2);
  Application.CreateForm(TfrmEntradaSistema, frmEntradaSistema);
  Application.CreateForm(TDataModuleGeral3, DataModuleGeral3);
  Application.CreateForm(TDataModuleRelatorios, DataModuleRelatorios);
  Application.Run;
end.
  • 1

    Edu, enter your project code (dpr) here. Also check if events are linked to the correct procedures.

  • In dpr the main form of the application is frmEntradaSystem. The two Forms that appear in your code are not being automatically created. From the code, you can see that you are coding actions of one form into another. That’s not very good. It creates a tangle that makes maintenance very difficult, especially when the project evolves. Possibly you are making an incorrect call, but it is not appearing in the code you posted. Things that may be happening:

  • The secondary form is the main application form (first Application.Createform); in the Object inspector one Handler may be assigned to the wrong event, from another form; a form that should be child is the other’s Owner.

1 answer

1

I went through it some time ago and in my case the form02.Parent := form01; the OnKeyDown, OnKeyPress and OnKeyUp form02 did not work and went to the respective Form01 events then I put everything in the event form01. I think it’s not the most elegant alternative more works and stayed like this.

procedure TfrmTelaPrincipal.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  // Aqui, mesmo não tendo esses componentes na form pai eu coloquei assim por que o nome do Componente e identificado 
  if ((key = VK_RETURN) or (key = VK_DOWN)) and not (Pos(Copy(TControl(ActiveControl).Name,1,3),'cbb.dbc.dbg.mem.dbm')>0) then
    keybd_event(VK_TAB, 0, 0, 0); // pressiona o Tab

  if (key = VK_UP) and not (Pos(Copy(TControl(ActiveControl).Name,1,3),'mem.dbc.cbb.dbg') > 0) then
  begin
    keybd_event(VK_SHIFT, 0, 0, 0);               // pressiona o Shift
    keybd_event(VK_TAB, 0, 0, 0);                 // pressiona o Tab
    keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0);   // solta o Tab
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0); // solta o Shift
  end;

  {$Region '  Referente aos Eventos do Form02. Passado para esse Evento pois no formulário filho não funciona  '}
  // Aqui eu coloquei para um componente especifico de cada Formulário 
  if (key = VK_RETURN) and (TControl(ActiveControl).Name = 'dbgConsultaClientes') then
  begin
    // Seu código aqui!!!
  end; {$EndRegion}
end; 

This way it worked and rotates until today.

  • Thanks for the help. I’ll do a test later! and warning if it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.