#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Virginia
    Posts
    4
    Rep Power
    0

    Program/Project Initialization


    I programmed in Delphi 4 years ago. In my code I had a "FormActivate" procedure that I used to initialize data and set stuff up. Now in XE4, my Main form cannot find a "FormActivate" method.

    What I would like to do is setup Public TADOConnection and other TADO objects to be used throughout the application. This requires that I .Create(Self) and set the values of the properties in code.

    Can somebody explain the methodology to me. I am just starting out on this project, so the code is limited on the main form. See the code attached.

    Thank you in advance.
    bill

    Code:
    unit KofC;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Menus, Data.DB,
      Data.Win.ADODB;
    
    type
      TfrmMain = class(TForm)
        MainMenu1: TMainMenu;
        F1: TMenuItem;
        Exit1: TMenuItem;
        InfoForms1: TMenuItem;
        KnightsWivesOthers500Base1: TMenuItem;
        N1: TMenuItem;
        NewBaseRecords1: TMenuItem;
        NewKofCNumbers1: TMenuItem;
        Accounting1: TMenuItem;
        CouncilDuesPayments1: TMenuItem;
        N500ClubPaymentsPayouts1: TMenuItem;
        Reports1: TMenuItem;
        Label1: TMenuItem;
        N2: TMenuItem;
        Accounting2: TMenuItem;
        CouncilDuesPayments2: TMenuItem;
        N500ClubPaymentsPayouts2: TMenuItem;
        Utilities1: TMenuItem;
        CouncilInfo1: TMenuItem;
        AccountingCodes1: TMenuItem;
        StatesandProvinces1: TMenuItem;
        itle1: TMenuItem;
        Suffix1: TMenuItem;
        YearlyDues1: TMenuItem;
        N3: TMenuItem;
        DBStructures1: TMenuItem;
        Help1: TMenuItem;
        About1: TMenuItem;
        imgKofCLogo: TImage;
        tempEDW2473Connection: TADOConnection;
        procedure Exit1Click(Sender: TObject);
        procedure CouncilInfoClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        KCconnect: TADOConnection;    {connection to data base}
      end;
    
    var
      frmMain: TfrmMain;
    
    implementation
    
    {$R *.dfm}
    
    { LINK in all forms associated with the project }
    uses CouncilInfo;
    
    
    
    
    procedure TfrmMain.Exit1Click(Sender: TObject);
    begin
      Application.Terminate;
    end;
    
    procedure TfrmMain.CouncilInfoClick(Sender: TObject);
    var
      CouncilInfoForm: TfrmKCInfo;
    begin
      CouncilInfoForm := TfrmKCInfo.Create(Self);
      try     CouncilInfoForm.ShowModal;
      finally CouncilInfoForm.Release;
      end;
    end;
    
    end.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    352
    Rep Power
    7
    If you want to make Database Components available to many different forms of you application, then possibly, creating a datamodule may help solve that problem. You can read up on datamodules here: http://delphi.about.com/od/database/l/aa101601a.htm or here: http://docwiki.embarcadero.com/RADStudio/XE4/en/Using_Data_Modules

    If you still prefer to pass the information from your main form, to child forms, then you could define a new "constructor" for your child form (TfrmKCInfo), pass it all the data you need to pass via that constructor. You can also declare a specialty record to pass all the information in one structure.

    Also, FormActivate is not the best solution to do initialization code, since that code would re-execute every time the form is reactivated (not an issue if the form is shown modal, like you have, but still not the best way to do so). Initialization code should be in the FormCreate and/or in the FormShow methods which only executes once for the form.

    To do so, in the child form (TfrmKCInfo) add the new record structure (in the same TYPE section as your form's declaraion) and the new constructor (in the public section of your form), and a variable to hold the values passed in (in the private section).
    Code:
    type
      //example record structure to pass in a connection/query/dataset component
      TInitializationParameters = record
        ADOConnection: TADOConnection;
        VarA: TMyOtherDataType;
      end;
    
      TfrmKCInfo = class(TForm)
        ...
        procedure FormCreate(Sender: TObject);
      private
        InitialParamters: TInitialParameters;
      public
        constructor CreateWithParamters(AOwner: TComponent; InitializationParamters: TInitializationParameters);
        ...
      end;
    
    constructor TfrmKCInfo.CreateWithParameters(AOwner: TComponent; InitializationParamters: TInitializationParameters);
    begin
      inherited Create(AOwner);
      InitialParamters := InitializationParamters;//now private InitialParameters variable has the passed in value.
                                                  //which can be accessed in the FormCreate Method.
    end;
    
    procedure TfrmKCInfo.FormCreate(Sender: TObject);
    begin
      //InitialParameters.ADOConnection - has a reference to the ADOConnection Component passed in.
      //InitialParameters.VarA - holds Value of VarA.
      ...
    end;
    To call your form change the reference to the .Create(Self) to CreateWithParameters(Self, MyFormParams) where MyFormParams is a local variable declared as a TInitializationParameters.
    Code:
    procedure TfrmMain.CouncilInfoClick(Sender: TObject);
    var
      CouncilInfoForm: TfrmKCInfo;
      MyFormParams: TInitializationParameters;
    begin
      MyFormParams.ADOConnection := KCconnect;
      MyFormParams.VarA := 'XXX';
      CouncilInfoForm := TfrmKCInfo.CreateWithParameters(Self, MyFormParams);
      try
         CouncilInfoForm.ShowModal;
      finally
         CouncilInfoForm.Release;
      end;
    end;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Virginia
    Posts
    4
    Rep Power
    0
    Thanks.
    Forgot about the Data Module, which I used before.
    Will give it a try.
    bill

IMN logo majestic logo threadwatch logo seochat tools logo