What is the easiest way to override the default value for a Delphi property?

I am trying to write a descendant of TCustomDBGrid that will look like a TListBox. One of the things I want to change is the properties of the Options properties. TCustomDBGrid defines the parameters as:

property Options: TDBGridOptions read FOptions write SetOptions default [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit]; 

Trying to override this in my class with

  property Options: TDBGridOptions default [dgTitles, dgTabs, dgRowSelect, dgAlwaysShowSelection, dgCancelOnExit]; 

does not work; the compiler expects to read or write after the type, and not by default . The problem is that FOptions and SetOptions are defined as closed, not protected in TCustomDBGrid.

Should I write my own get and set methods that call " inherited parameters", or is there an easier way to do this?

+4
source share
2 answers

Do not specify type. Then you can change the default value. And remember that you also need to set the Options property for this default value in the constructor. The code below does not actually set a default value, it just allows it not to pass the property value when the value looks like this.

 property Options default [dgTitles, dgTabs, dgRowSelect, dgAlwaysShowSelection, dgCancelOnExit]; 
+10
source

This will not work in the dbgrid runtime ... See This (from the Delphi Language Guide on BDS 2006):


Property values ​​are not automatically initialized to the default value. That is, the default directive only controls when property values ​​are stored in the form file, but not the initial property value in the newly created instance.


For components in the form during development, I think there are no problems. But during the execution of the created controls, I think it is better (I mean, safer) to make an override on the constructor:

 constructor Create(Aowner:TComponent); begin inherited; Options := [dgTitles, dgTabs, dgRowSelect, dgAlwaysShowSelection, dgCancelOnExit]; end; 

To keep everything in order, also follow the default directive:

 property Options default [dgTitles, dgTabs, dgRowSelect, dgAlwaysShowSelection, dgCancelOnExit]; 
+2
source

All Articles