Link to home
Start Free TrialLog in
Avatar of hakanfa
hakanfaFlag for Finland

asked on

Form Close

Hello!

Is it possible to determine if a form is close from the Title Bar or if it is
programacally closed?

Hokki
Avatar of mokule
mokule
Flag of Poland image

If You mean programatically - by Your aplication.
This would work.

  private
    bCloseType: boolean;


procedure TForm1.FormCreate(Sender: TObject);
begin
  bCloseType := False;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  bCloseType := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  bCloseType := True;
  Close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if bCloseType then
    Application.MessageBox('Closed programatically','Info',MB_OK);
end;
Avatar of shaneholmes
shaneholmes

hakanfa,

var
  bClose: Boolean = False;


procedure TForm1.Button1Click(Sender: TObject);
begin
 bClose: True;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 if bClose then
  ShowMessage('Closed by button!')
 else
  ShowMessage('Closed by Form!');
end;



If you are trying to prevent a user from closing the form from the Title Bar buttons, then you could do this:


var
  Form1: TForm1;

procedure TForm1.Button1Click(Sender: TObject);
begin
 bClose: True;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 if bClose then
 begin
  ShowMessage('Closed by button!');
  CanClose:= True;
 end
 else
 begin
  ShowMessage('Closed by Form!')
  CanClose:= False;
 end;
end;


Of course, the other way would be to set the form's BorderIcon's property (biSystemMenu) to false, and set the BorderStyle property to bsSingle.

Hope this helps!

Shane
Avatar of hakanfa

ASKER

Well actually i figured out by my self...

  private
    { Private declarations }
    procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
.....

procedure TFrmLogin.WMSysCommand;
begin
  if Msg.CmdType = SC_CLOSE then
  begin
{Your code here}  
  end
  else
  begin
    inherited;
  end;
end;
How do You call it programatically close or from title bar ? :)

  SendMessage(Form1.Handle,WM_SYSCOMMAND,SC_CLOSE,0);
Avatar of hakanfa

ASKER

Hello!
What I was looking for was to find out if a form is closed from the "Titlebar", and
the above code figures it out. If the user presses the(x) on the title bar the code
in {your code section} fires. Maybe I was not specific enough in my question.
Sorry for that!

Thanks anyway for You support pals,

Hokki
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial