Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching forms in delphi [duplicate]

Possible Duplicate:
Delphi application with login / logout - how to implement?

I am trying to switch between two forms in my delphi application, first, a login screen appears and then the main form of the application appears.

I am using formx.hide and .show to switch between the forms. eg. //after password checking form1.hide; form2.show;

The second form appears, but cannot be interacted with, as if it is disabled. Why would it be doing this?

like image 222
user1726682 Avatar asked Feb 19 '23 22:02

user1726682


1 Answers

Since you have not provided any code, we have to guess at what your problem is. So here goes.

Forms get disabled when other forms are shown modally, and then re-enabled when the modal form is closed. So most likely you show the login form modally and then hide it rather than close. To close the modal form you need to set the modal form's ModalResult property. If you hide rather than close, then the main form will still be disabled. The key is that you must properly close the modal form before the main form can become usable.

Typically for an app with an initial login form you organise your application's .dpr file like this:

var
  LoginForm: TLoginForm;
  MainForm: TMainForm;
  LoginSucceeded: Boolean;

begin
  Application.Initialize;
  LoginForm := TLoginForm.Create(nil);
  try
    LoginForm.ShowModal;
    LoginSucceeded := LoginForm.Successful;
  finally
    LoginForm.Free;
  end;
  if LoginSucceeded then
  begin
    Application.CreateForm(TMainForm, MainForm);
    Application.Run;
  end;
end;

The first form created using Application.CreateForm becomes your applications's main form. When the main form is closed, the entire application goes down with it. In my opinion, you should use Application.CreateForm only for creating the main form. Any other forms can be created using the TMyForm.Create syntax. If you follow that policy then you don't need to worry about which order your forms are created in.

like image 74
David Heffernan Avatar answered Feb 28 '23 02:02

David Heffernan