Solved

Form1 triggering a button click on another form fails

Posted on 1997-11-18
5
535 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now