Search code examples
formsdelphidelphi-7

delphi..Onactive button disble confusion for a available form


hey im working on delphi 7, and i have a scenario for a available form, i have two forms

  1. FormOne 
  2. FormTwo

FormOne has a button

enter image description here

which create the FormTwo on button click

    var
     Form2:TForm2;
      begin
       Form2:=TForm2.Create(nil);
       Form2.ShowModal ;
        Form2.Free;
     end;

On Form2 there is a button which i need to disable..(on some conditions).. enter image description here

so on activate of Form2 i did this

       if assigned(Form2) then
        begin
         Form2.Button1.Enabled:=False;
        end;

that is im checking if the form is created then disable the button.. since the code is in Onactivate meaning the form2 is already created according to this the delphi form liyfe cyle is

OnCreate -> OnShow -> OnActivate -> OnPaint -> OnResize -> OnPaint

..so the button1 should be disabled..but it NOT disabled.

enter image description here


Solution

  • I guess Form2 is a local variable in your button click handler; in your OnActivate handler your are testing a global Form2 variable from the unit where TForm2 is defined; the second is not assigned if TForm2 is not autocreated form.