views:

1358

answers:

3

I'm trying to write a TCustomDBGrid descendant that's designed to feel like a TListBox. One of the things I want to change is the Options property's defaults. TCustomDBGrid defines Options as:

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

Trying to override that in my class with

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

doesn't work; the compiler expects read or write after the type, not default. Problem is, FOptions and SetOptions are both defined as private, not protected, in TCustomDBGrid.

Do I have to write my own get and set methods that invoke "inherited Options", or is there a simpler way to do this?

+6  A: 

Don't specify the type. Then you can change the default. And remember that you also have to set the Options property to this default in the constructor. The code below does not actually set the default, it just lets it know to not stream the property value when the value is like that.

property Options default [dgTitles, dgTabs, dgRowSelect, dgAlwaysShowSelection, dgCancelOnExit];
Lars Truijens
Thanks! I knew there had to be some simple way to do it...
Mason Wheeler
A: 

I posted this same question on CodeGear's forums. StackOverflow got me an answer in 7 minutes, covering all the details in 4 lines. Embarcadero Discussion Forums got me the same substantive answer, explained over 2 pages, in 15 minutes.

StackOverflow wins!

Mason Wheeler
It's not a competition. I'd have given you the same answer here, had I seen it here first. And if my answer spread over two pages, either your pages are tiny or your text is huge.
Rob Kennedy
+2  A: 

That would not work in a runtime created dbgrid... See this (from Delphi Language Guide on BDS 2006):


Property values are not automatically initialized to the default value. That is, the default directive controls only when property values are saved to the form file, but not the initial value of the property on a newly created instance.


For components on a form in design time, I think there's no trouble. But in runtime created controls, I think it's better (I mean, safer) to do the override on the constructor:

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

To make things right, do the default directive also:

property Options default  [dgTitles, dgTabs, dgRowSelect, dgAlwaysShowSelection, 
                           dgCancelOnExit];
Fabricio Araujo
Actually you need to do both to do it right. But this part needs not to be forgotten or it won't work at all. I did pointed it out in my answer, but not with code like you did.
Lars Truijens
Ok. As most of my coding goes to non-visual classes, I don't bother so much with default directive (except for array properties, where this directive is MUCH more useful)... ;-)
Fabricio Araujo