Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to check if a modal form exists?

Posted on 2004-10-27
11
Medium Priority
?
247 Views
Last Modified: 2010-05-18
Hi,

I am trying to write a centralized (in a seperate unit) procedure that manages the creation, display and release of forms. The slimmed down version of my procedure looks roughtly like this:

procedure OpenAForm(Sender: TObject);
var
  f:TForm;
begin
  // check if frmMain exists
  if frmMain <> nil then
  begin
    if Sender = frmMain.mnuAbout then f := TfrmAbout.Create(frmMain);
    if Sender = frmMain.mnuProgramSettings then f := TfrmProgramSettings.Create(frmMain);
    if Sender = frmMain.mnuFSAdvancedSetup then f:= TfrmFSAdvancedSetup.Create(frmMain);
  end;

  // check if frmFSAdvancedSetup exists
  if frmFSAdvancedSetup <> nil then
  begin
    if Sender = frmFSAdvancedSetup.cmdManagef := TfrmFSManagement.Create(frmFSAdvancedSetup);
  end;

  try
    f.ShowModal;
  finally
    f.Release;
  end;
end;


The way the forms are shown on screen is as follows: frmMain displays modal form frmFSAdvancedSetup, which in turn displays a modal form frmFSManagement. Checking the existance of the main form goes fine, but even if frmFSAdvancedSetup is on screen, it returns nil, thereby crashing my app with an Access Violation.

So, how exactly can I make sure that modal forms are detected as being onscreen. This procedure looked fine in theory, but reality turned out a bit less succesful. Any help appreciated.

-edit: slimmed the code down a bit too much, corrected-
0
Comment
Question by:wileyecoyote
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12427712
function ShowForm(FormClass: TFormClass):Integer;
var
  Reference: TForm;
begin
  result := mrCancel;
  Reference := FormClass.Create(nil);
  if Assigned(Reference) then begin
    try
      Reference.ShowModal;
      result :=  Reference.ModalResult;
    finally
      Reference.Release;
    end
  end
end;

0
 

Author Comment

by:wileyecoyote
ID: 12427906
Hmmm, it still crashes when I'm trying to display that second modal form (frmFSManagement). Again, it halts the program with a Access Violation. To be precise, it crashes on the Reference.ShowModal line. Any idea what might be causing it?
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12427946
1)may be it is autocreated form
2)may be it crashes in onShowEvent
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:esoftbg
ID: 12428024
procedure  ShowModalfrmFSAdvancedSetup;
var
  N:      Integer;
  I:      Integer;
begin
  N := 0;
  for I := 0 to (Screen.FormCount-1) do
    if (Screen.Forms[I] is TfrmFSAdvancedSetup) then
      N := I;
  try
    if (N >= 0) then
      Screen.Forms[N].ShowModal
    else
    begin
      frmFSAdvancedSetup := TfrmFSAdvancedSetup.Create(nil);
      frmFSAdvancedSetup.ShowModal;
    end;
  finally
    frmFSAdvancedSetup.Release;
    frmFSAdvancedSetup := nil;
  end;
end;
0
 

Author Comment

by:wileyecoyote
ID: 12428276
Close, but there is still a bug in here somewhere. The app gets within the If statement right to the ShowModal line, where it shouldn't. I'll take a look at it in the morning (it's 1AM now here). Focus seems to be getting lost now. See you again in the morningn.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12428340
O.k. see you morning .... (Here is 02:04 am)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12428366
Oooops !
procedure  ShowModalfrmFSAdvancedSetup;
var
  N:      Integer;
  I:      Integer;
begin
  N := -1; // this was wrong above
  for I := 0 to (Screen.FormCount-1) do
    if (Screen.Forms[I] is TfrmFSAdvancedSetup) then
      N := I;
  try
    if (N >= 0) then
      Screen.Forms[N].ShowModal
    else
    begin
      frmFSAdvancedSetup := TfrmFSAdvancedSetup.Create(nil);
      frmFSAdvancedSetup.ShowModal;
    end;
  finally
    frmFSAdvancedSetup.Release;
    frmFSAdvancedSetup := nil;
  end;
end
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12428399
This should be the best:

procedure  ShowModalfrmFSAdvancedSetup;
var
  N:      Integer;
  I:      Integer;
begin
  N := -1; // this was wrong above
  for I := 0 to (Screen.FormCount-1) do
    if (Screen.Forms[I] is TfrmFSAdvancedSetup) then
      N := I;
  try
    if (N >= 0) then
      Screen.Forms[N].ShowModal
    else
    begin
      frmFSAdvancedSetup := TfrmFSAdvancedSetup.Create(nil);
      frmFSAdvancedSetup.ShowModal;
    end;
  finally
    if (N >= 0) then
    begin
      Screen.Forms[N].Release;
      Screen.Forms[N] := nil;
    end
    else
    begin
      frmFSAdvancedSetup.Release;
      frmFSAdvancedSetup := nil;
    end;
  end;
end;
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 375 total points
ID: 12428451
Never say that something is the best. (It could be improved and then it becomest better from the best one .... ha, ha, ha):

procedure  ShowModalfrmFSAdvancedSetup;
var
  N:      Integer;
  I:      Integer;
begin
  N := -1;
  for I := 0 to (Screen.FormCount-1) do
    if (Screen.Forms[I] is TfrmFSAdvancedSetup) then
      N := I;
  if (N >= 0) then
  try
    Screen.Forms[N].ShowModal
  finally
    Screen.Forms[N].Release;
    Screen.Forms[N] := nil;
  end
  else
  try
    frmFSAdvancedSetup := TfrmFSAdvancedSetup.Create(nil);
    frmFSAdvancedSetup.ShowModal;
  finally
    frmFSAdvancedSetup.Release;
    frmFSAdvancedSetup := nil;
  end;
end;
0
 

Author Comment

by:wileyecoyote
ID: 12431021
Well, you can't assign nil to a Screen.Forms[N] property, but that's okay. I'll rebuild the function anyway so it takes not only frmFSAdvancedSetup, but other forms as well. I'll fix that one in the process.

Thanks for the help!
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12431222
Hi wileyecoyote,
sorry about     Screen.Forms[N] := nil;
it was 02:20am EEST and I did a couple of mistakes ....
excuse me, i'm glad that you solved all !
Best regards,
emil
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question