Solved

Form1 triggering a button click on another form fails

Posted on 1997-11-18
5
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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