Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to give control to main after free a modal form? 1

Status
Not open for further replies.

smilife

Programmer
Dec 1, 2003
30
CN
example:
procedure TForm1.Button1Click(Sender: TObject);
begin
form2 := TForm2.Create(Self);
form2.ShowModal;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
FreeAndNil(Form2);
//i want to give control to main window here, but i cannt.
end;

I want to give control to main thread after FreeAndNil a Modal window.
how can i do?

thanks.
 
Put the TTimer into Form2. When the timer event occurs in Form2 set the ModalResult to mrOK (or whatever you want) and then free Form2 in Form1.

So your Button1 OnClick event handler should look something like this:
Code:
procedure TForm1.Button2Click(Sender: TObject);
var
  form2: TForm2;
begin
  form2 := TForm2.Create ( self );
  try
    form2.ShowModal;
// Control returns here when Form2 closes
  finally
    form2.free;
  end;
end;
Andrew




 
Thanks for Towerbase.

I still have a question:
why have to put the TTimer into form2? I have test as follow code:

procedure TForm1.Button1Click(Sender: TObject);
begin
if Form2 = nil then
Form2 := TForm2.Create(Self);
Form2.ShowModal;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Form2.ModalResult := mrOK;
FreeAndNil(Form2);//here error
end;
example 1

why a Modal form have already been closed, but cannt be Free? but a modalless form can, code as follow:

procedure TForm1.Button1Click(Sender: TObject);
begin
if Form2 = nil then
Form2 := TForm2.Create(Self);
Form2.Show;
FreeAndNil(Form2);//here have no error
example 2

I feel puzzled.

how the modal form works? I found after "freeAndNil(Form2)" in example 1, program return "Form2.ShowModal".Why it do so?
 
The difference between a modal form and a non-modal form is that control is given to the modal form and processing stops in the calling form until the modal form returns control to the calling form.


Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top