Jump to content
bazzer747

Closing Forms from other forms

Recommended Posts

Hi,

 

My application has a series of 3 forms, one where a User views some names, a second where the User selects some of those names, and a third where the User can view the names and manipulate them. I create all the forms modally, which means when I get the 3rd form those other 2 forms are still visible but are not required any longer. But the User can only close them when he closes the 3rd form. Also, it's not very good UI for him to go through that process. What I'd like is for the 3rd form, when it is opened, to close the previous two forms.

 

What I've tried is this code in FormActivate:

var
  MyHandle: tHandle;
begin
//If you came from Select Teams then close that window ...
MyHandle:= FindWindow( nil, 'Select Team' );

SendMessage( MyHandle, WM_CLOSE, 0, 0 );
Application.ProcessMessages;

 

Which only seems to work AFTER I close the 3rd form. Is there a way to close the previous forms as soon as the 3rd form opens?

 

 

 

Edited by bazzer747
Missed 2 lines of code

Share this post


Link to post

Don't create dependencies between unrelated forms (or even related ones but that's another matter).

It's hard to advise you when I don't know the architecture of your application but let's assume that it's a small application with a main form and the three forms you mentioned.

Decide on where the controller role should lie. In a small application this can just be the main form. Make sure the controller knows the state of the different forms it controls. Only open and close forms via the controller. Now it should be a simple job of  closing the two other forms if the third is opened. You may need to use postponed close with messages or TForm.Release depending on how the lifetime of your forms are managed.

Share this post


Link to post

Anders,

 

Yes, it's a small application (mind there are about 30 different forms).

The 3 forms I'm talking about lead naturally from one to the other - from the main form the first form opens viewing a list of names - if there are enough names the 2nd form is selected, this 2nd form selects from those names and leads to viewing them in the 3rd form (where many other operations can be done). It would be clumsy to do the opening (and closing) from the main form, going back then forward. If it is possible to close the form you have just come from, then that seems to me to be a much more efficient and better User experience.

Share this post


Link to post
22 minutes ago, bazzer747 said:

If it is possible to close the form you have just come from, then that seems to me to be a much more efficient and better User experience.

 

const
  MSG_CLOSE = WM_USER;

type
  TMyForm = class(TForm)
  private
    procedure MsgClose(var Msg: TMessage); message MSG_CLOSE;
  public
    procedure DoIt;
  end;

procedure TMyForm.MsgClose(var Msg: TMessage);
begin
  Close;
end;

procedure TMyForm.DoIt;
var
  NextForm: TMyNextForm;
begin
  NextForm := TMyNextForm.Create(Application);
  
  // Close deferred
  PostMessage(Handle, MSG_CLOSE, 0, 0);

  // Open next form
  NextForm.Show; // or ShowModal
end;

 

If your form is destroyed when it is closed then you can just use TForm.Release instead.

Share this post


Link to post
On 5/5/2020 at 10:10 PM, bazzer747 said:

The 3 forms I'm talking about lead naturally from one to the other

You are describing a wizard UI...

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

×