Jump to content
Ian Branch

How to detect if a Dialog is open/running?

Recommended Posts

Hi Team,

I have a Timer routine that runs every minute.

The routine detects if there is an AlertMessage.txt file present and if there is, opens it, populates then executes the modal dialog.

It does this every minute.

If the User doesn't acknowledge the first dialog another will be popped up on top.  And so on..

Is there some way to detect if the original dialog is still open so subsequent executions don't occur?

 

Regards & TIA,

Ian

Share this post


Link to post

This should get you started, I match mine by classtype.

  for i := 0 to Screen.FormCount - 1 do // itterate the screens
    if Screen.Forms[i].ClassType = TSERTTKMarshalForm then
    begin
      Screen.Forms[i].Show;
      if Screen.Forms[i].WindowState = TWindowState.wsMinimized then
        Screen.Forms[i].WindowState := TWindowState.wsNormal;
      exit;
    end;

 

Share this post


Link to post

I'm not sure I understand your description, but you might:

  • Check Application.ModalLevel to determine if a (Delphi) modal dialog is active.
  • Check Screen.ActiveForm to determine exactly which form is currently active.

or simply avoid the problem altogether by not executing the timer code while a modal dialog is active:

procedure TMyForm.TimerAlertMessageTimer(Sender: TObject);
begin
  TTimer(Sender).Enabled := False;
  try
    var FormAlertMessage := TFormAlertMessage.Create(Self);
    try
      FormAlertMessage.ShowModal;
    finally
      FormAlertMessage.Free;
    end;
  finally
    TTimer(Sender).Enabled := True;
  end;
end;

or

procedure TMyForm.TimerAlertMessageTimer(Sender: TObject);
begin
  if (Application.ModalLevel > 0) then
    exit; // Modal dialog is active; Punt!

  var FormAlertMessage := TFormAlertMessage.Create(Self);
  try
    FormAlertMessage.ShowModal;
  finally
    FormAlertMessage.Free;
  end;
end;

 

  • Like 1

Share this post


Link to post

Thanks Guys for your suggestions.

Doh!!  So simple!  Just disable/enable the timer...

I'm embarrassed. 🙂

 

Regards & Tks Again,

Ian

Share this post


Link to post

Depending on your needs a non-modal solution might be better - something like Windows' toast notifications. I use DevExpress' dxAlertWindow for that.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×