[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Form1 triggering a button click on another form fails

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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