?
Solved

Form1 triggering a button click on another form fails

Posted on 1997-11-18
5
Medium Priority
?
542 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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