开发者

In Delphi, how do you assign a component's property values before creating an app's main form?

开发者 https://www.devze.com 2023-04-05 02:27 出处:网络
TMS\'s FormSize component saves aForm\'s size and position in an .ini file. This file\'s path is开发者_Go百科 stored in the component\'s SaveName property. I would like to assign FormSize.SaveName to

TMS's FormSize component saves aForm's size and position in an .ini file. This file's path is开发者_Go百科 stored in the component's SaveName property. I would like to assign FormSize.SaveName to a file in the user's AppData folder. I can find the AppData path in my source code.

Does anyone know where (in my code) I assign the AppData path to FormSize.SaveName? I am thinking the FormSize component is created, and a default SaveName initialized, BEFORE aForm is created. In other words, FormSize loads the config file BEFORE aForm's FormCreate event fires; assigning a value to FormSize.SaveName during aForm.FormCreate is too late.

Thanks, as always.


The adjustment of the form is done in the Loaded method of TFormSize, not when you change the SaveName property (although it has been read from the DFM before).

If you set the properties SavePosition and SaveSize to false during designtime, there will nothing be loaded at runtime. In that case you can manually load and save the settings at a convenient place in your code by calling LoadFormSettings and SaveFormSettings.


I'd expect SaveName to be stored in the .dfm file, so it should be assigned to the component at load up.

If you want to determine the save name in code, it should probably be early in the cycle. I just checked a few possibilities:

  1. In the form's constructor (override), before the call to inherited;
  2. in the form's constructor (override), after the call to inherited;
  3. in the form's FormCreate event handler;
  4. in the form's Loaded procedure (override), before the call to inherited and
  5. in the form's Loaded procedure (override), after the call to inherited.

Possibilities 4 and 5 worked as expected. 3 and 2 did nothing and 1 caused an AV. So David's suggestion seems to be fine.


"assigning a value to FormSize.SaveName during aForm.FormCreate is too late."

I had a similar requirement to modify a component's property owned by a module. The standard "Create" event was too late given the loaded property was already in effect.

Properties persisted in the DFM are assigned (or cached) during the call to the protected virtual ReadState procedure. Conventionally, cached properties are activated during the protected virtual call to Loaded. Both ReadState and Loaded can be overridden.

In my case, I wanted to make sure TADOConnection's Connected property was false in the release build. During development, the component's property is normally true given design needs of dependent data sets.

It was a pain having to set the property to false prior to checking-in the code for subsequent builds/deployment. So instead, I overrode the Loaded method and hacked the streamed property value to false.

interface

type
    TMyDataModule = class(TDataModule)
        MyAdoConnection: TADOConnection;
    protected
        procedure Loaded; override;
    end;

implementation

type
  TADOConnectionHack = class(TADOConnection) end;

procedure TMyDataModule.Loaded;
begin
  TADOConnectionHack(MyAdoConnection).StreamedConnected := False;
  inherited Loaded;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号