Closing a TSaveDialog from the code lazarus / 31-LCL ---------------------------------------------------------------------- Relativity · 2015-11-24 11:00 In my application I have a TSaveDialog showing up (with TSaveDialog.Execute) under certain circumstances and a TTimer running. When the TTimer time runs out, a MessageDlg shows up, the user must click "OK" and then I would like the application to close the TSaveDialog (if it is showing) by itself (that is, from code). I tried using TSaveDialog.Close (in the OnTimer method, clear) but the TSaveDialog persists. Do you know the way ? Or is maybe such a feature not provided for at all ? ---------------------------------------------------------------------- balazsszekely · 2015-11-24 13:51 I see you have windows...Set Timer interval to 10 sec or more and try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  //... TForm1 = class(TForm)   //... private   NeedToClose: Boolean;   //... end; //..uses windows; function FindWindowX(const APHandle: THandle): THandle;var  wHandle: THandle;begin  Result := 0;  wHandle := GetWindow(APHandle, GW_HWNDFIRST);  while wHandle > 0 do  begin    if IsWindowVisible(wHandle) then    begin      if GetParent(wHandle) = APHandle then        Result := wHandle;    end;    wHandle := GetWindow(wHandle, GW_HWNDNEXT);  end;end; procedure TForm1.Timer1Timer(Sender: TObject);var  wHandle: THandle;begin  Timer1.Enabled := False;  wHandle := FindWindowX(Form1.Handle);  if (wHandle <> 0) and (NeedToClose) then  begin    ShowMessage('Save dialog is going down...');    PostMessage(wHandle, WM_CLOSE, 0, 0);  end;end; procedure TForm1.Button1Click(Sender: TObject);begin  Timer1.Enabled := True;  NeedToClose := True;  if SaveDialog1.Execute then  begin    //user clicked ok  end;  NeedToClose := False;end; PS: Added boolean NeedToClose to prevent accidents. ---------------------------------------------------------------------- Ocye · 2015-11-24 13:53 Modal dialogs loop events internally- hope to phrase this correct, anyway the main loop events are not executed. That means even when you set the modal result in timer execute, which is the right way to close the modal dialog, it wouldn't be satisfying. And if you show an amodal dialog you have to think about what happens when a second one pops up. Furthermore, you should provide feedback to user what happens. In case of a save dialog it makes IMHO no sense to close it programmatically. But for confirmations it might be useful. Here is my solution: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { TTimeOutDialog }var  aDialog1,aDialog2:TForm; function TimeoutMsg(const aMessage: string; const aTimeOut: word): TModalResult;var aStart,aPast   : longword;    aBitmap        : TCustomBitmap;    x              : integer;    pDialog        : ^TForm;begin  //close old dialog if a new one is opened  if assigned(aDialog1) then  begin    aDialog1.ModalResult:=mrCancel;    aDialog1.Hide;    aDialog1:=nil;    pDialog:=addr(aDialog2);  end else  begin    if assigned(aDialog2) then    begin      aDialog2.ModalResult:=mrCancel;      aDialog2.Hide;      aDialog2:=nil;    end;    pDialog:=addr(aDialog1);  end;  if aTimeOut=0 then    exit; //just cancel dlg   pDialog^:=CreateMessageDialog(aMessage,mtConfirmation,[mbYes,mbNo]);  with pDialog^ do  try    FormStyle:=fsStayOnTop;    Position:=poMainFormCenter;    Show;//not modal    aStart:=GetTickCount;    aBitmap:=GetDialogIcon(idDialogConfirm);    aBitmap.Canvas.Brush.Style:=bsSolid;    aBitmap.Canvas.Pen.Style:=psClear;    aBitmap.Canvas.Brush.Color:=clBtnFace;    x:=0;    repeat      aPast:=(GetTickCount-aStart) div 1000;      if (aPast>0) and (aPast{%H-}<>x) then      begin        Canvas.Brush.Color:=clGreen;      Canvas.FillRect(Bounds(0,0,round(Width{%H-}*aPast/aTimeOut),2));      if aTimeOut-aPast<10 then          Caption:=Language.Plural(rPoll_TimeOut,aTimeOut-aPast);        x:=aPast;      end;      Enabled:=ModalResult<>mrCancel;      try      sleep(1);        Application.ProcessMessages;      except        raise;      end;      if Application.Terminated then        ModalResult:=mrCancel;      if ModalResult<>mrNone then        break;      if (aPast>=aTimeOut) then        ModalResult:=mrTimeout;    until (ModalResult<>mrNone) or (Visible=false);    Result:=ModalResult;  finally    aBitmap.Free;    Free;    pDialog^:=nil;  end;end;I'm pretty sure the experts would beat me round my head with this code ;-).