Hello,

Im making a program which when you run it opens up a small form which asks for your password and username, if its incorrect it shows Alert message but if it's correct closes the first form and opens second form with the stuff...

so My question is - How Can I make program run like when second form opens (form2.show) first form closes (form1.close) because all the time form1.close all the program not the form?

Dawiss,
are you agree about: Form1 to ask for the password and if password is correct on pressing a Button1 to show Form2 ?

( The reason is that usualy Form1 is the main form of the Application and when the Main Form closes it closes the Application.
I think it is possible the case you describe, but it is more difficult .... )
Project1

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

Form1

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (Edit1.Text='correct password') then
  begin
    Form1.Visible := False;
    Form2.ShowModal;
  end;
end;

end.

Form2

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form1.Close;  // Closes Application
end;

end.

First: make form2 your main form.
Second: set visible on form2 to false.
Third: form2.OnShow ----> form1.showmodal
Forth: on form1 if password and user are ok then
form2.visible:=True;
form1.close;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.