Solved

Form1 triggering a button click on another form fails

Posted on 1997-11-18
5
537 Views
Last Modified: 2010-04-04
I am using Delphi 1.0...

I have one main form with a button and a timer component on it.  When the button on Form1 is clicked, the timer is enabled and then I create Form2 and ShowModal it.  When the timer event triggers, I want Form2 to be closed automatically, and then I want a message to appear stating "Time is up!".  The code below is close to what I want, but not quite.  The problem is, the message dialog is appearing BEFORE the Form2 is closed, which seems odd to me.  How do I ensure that Form2 is closed before the MessageDlg is displayed?

procedure TForm1.Button1Click(Senter: TObject);
begin
      Timer1.Enabled := True;
      Form2:= TForm2.Create(Application);
      try
            Form2.ShowModal;
      finally
            Form2.Free;
      end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
      Timer1.Enabled := False;
      if Assigned(Form2) then begin
            Form2.Button1.Click;
      end;
      MessageDlg('Time is up!',mtInformation, [mbOk], 0);
end;

I want to force the Form2 to close SHOULD it be open when the timer on Form1 triggers.
0
Comment
Question by:Kapusta
5 Comments
 
LVL 1

Expert Comment

by:anilms
ID: 1350876
The timer usually queues up all messages and executes them one by one. Your program just calls Button1.Click and does not wait for a return, it just goes ahead with the next instruction which is your MessageDlg. There are two ways to solve this - have your Button1Click return a value and check for this value before proceeding - or add a while loop as given :

if Assigned(Form2) then
begin
     Form2.Button1.Click;
     While Assigned(Form2) do
     begin
          {do something or nothing at all - just waiting}
     end
end;
MessageDlg....
0
 

Author Comment

by:Kapusta
ID: 1350877
Thanks for your response, however unfortunately neither suggestion works.  In fact, the second suggestion locked up my PC tight as a drum since I went into While/do loop with nothing inside of it.  Even when I added Application.ProcessMessages inside the loop, it still did not solve my problem.  The Form2 cannot be closed automatically...I still must manually close it with a physical mouse click on Form2.Button1.

I also tried creating a global variable, and toggling it within Form2's Button1 Click event, but then checking it back within Form1's Timer1 event, but for some reason, Form2 still remained open.

Form2's Button1Click event looks like this:

begin
 Form2IsOpen := False;
 Close;
end;

Any other ideas?

0
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 1350878
Hi Kapusta,

This should work :

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
 Timer1.Enabled := True;
 Form2:= TForm2.Create(Application);
 try
  Form2.ShowModal;
 finally
  Form2.Free;
 end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 Timer1.Enabled := false;
 if assigned(form2) then
  begin
   form2.close;
   messagedlg('Time is up!',mtinformation,[mbOk],0);
  end;

end;

end.

Have fun,
c.u. ZifNab;
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1350879
 Hi Kapusta, I don't want to repeat myself, but here goes:

1)You'll STILL have to add the line:
  Form2 := Nil;
  Try it without, the message will always show.

2)The easiest way to get Form2 to vanish before the message is displayed is with:
  Form2.Hide;



  Try this variation on ZifNabs suggestion:  


procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled := True;
  If Not Assigned(Form2) then Form2 := TForm2.Create(Application); // Oh yeah, its best to make this check also
  try
    Form2.ShowModal;
  finally
    Form2.Free;
    Form2 := Nil;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := false;
  if Assigned(Form2) then
  begin
    Form2.Hide;
    Form2.Close;
    Form2 := Nil;
    MessageDlg('Time is up!',mtInformation,[mbOk],0);
  end;
end;


  Steven.
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1350880
 You're welcome.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 190
Comparing dates in Delphi. Greater than/ Less than 3 154
Delphi selector screen 2 80
Dynamically Created Query 3 55
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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