Autoclose form.

Hello, I have this issue. A form that runs only a progress bar (until now, just only a for loop to test it), that is going to do a sincronization task. The form is declared with these properties values: BorderStyle = bsDialog or bsToolWindow, Position = poDesktopCenter, BorderIcons = []. The rest remains default.
This form is execute with showmodal in the ButtonCloseClick event of the main form of the application.

The only code it has in is this:
procedure TfrmSincronizationTask.FormActivate(Sender: TObject);
var i: integer;
begin
  for i := 0 to pgSincronization.Max do
    pgSincronization.StepIt;
  ModalResult := mrOk;
end;

The form is called in this way:
      frmSincronizationTask := TfrmSincronizationTask.Create(Application);
      frmSincronizationTask.ShowModal;
      frmSincronizationTask.Free;
      frmSincronizationTask := nil;

What i need is the form closing automatically when the task is finished, but it don't.

I will appreciate some help
Thanks you in advance,
LVL 4
Martin BarredaLic. in Computer ScienceAsked:
Who is Participating?
 
esoftbgCommented:
type
  TfrmSincronizationTask = class(TForm)
      pgSincronization: TProgressBar;
    TimerSincronization: TTimer;
      procedure FormActivate(Sender: TObject);
      procedure TimerSincronizationTimer(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
  end;

var
  frmSincronizationTask: TfrmSincronizationTask;

implementation

{$R *.dfm}

procedure TfrmSincronizationTask.FormActivate(Sender: TObject);
var
  I:      Integer;
begin
  for I := 0 to pgSincronization.Max do
    pgSincronization.StepIt;
  TimerSincronization.Enabled := True;
end;

procedure TfrmSincronizationTask.TimerSincronizationTimer(Sender: TObject);
begin
  Close;
  ModalResult := mrOk
end;

end.
0
 
Martin BarredaLic. in Computer ScienceAuthor Commented:
I consider the question easy, but urgent, so the points...
0
 
_Katka_Commented:
Hi, try to move your code into FormShow instead of FormActivate
I guess it's not possible to apply ModalResult in Activation event.

regards,
Kate
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
esoftbgCommented:
object frmSincronizationTask: TfrmSincronizationTask
  Left = 256
  Top = 128
  BorderIcons = []
  BorderStyle = bsToolWindow
  Caption = 'Sincronization Task'
  ClientHeight = 158
  ClientWidth = 504
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnActivate = FormActivate
  PixelsPerInch = 96
  TextHeight = 13
  object pgSincronization: TProgressBar
    Left = 8
    Top = 64
    Width = 480
    Height = 17
    Max = 10000
    TabOrder = 0
  end
  object TimerSincronization: TTimer
    Enabled = False
    Interval = 100
    OnTimer = TimerSincronizationTimer
    Left = 34
    Top = 18
  end
end
0
 
BlackTigerXCommented:
if you call it with ShowModal it will stop any other processing in the main form, you might consider something like this:

frmSincronizationTask := TfrmSincronizationTask.Create(Application);
try    
  frmSincronizationTask.Show;
  doYourProcessingHere();
finally
  frmSincronizationTask.Free;
  frmSincronizationTask := nil;
end;
0
 
Martin BarredaLic. in Computer ScienceAuthor Commented:
_Katka_: it does not work.
BlackTigerX: i do not need to do any processing in the main form, only in the new form. It might work but is better for my porpouse that the code of the sincronization task keeps in the sincronization form, separeted from the original code.
esoftbg: i am looking for a more elegant solution (i tried something like that with an invisible button and do not work), but what you say, works. So i will give you the points for the efforts on answering the question. What is more, elegant or not, is what i am going to use... THANKS!!!
0
 
esoftbgCommented:
spk2000ar, you are welcome !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.