Reputation: 2461
I wonder if it is possible to define a default property value to a component.
In another words, I want to set, in design time, an unique name (maybe GUID) to each TDBGrid in the system, is it possible?
There is another way to control uniqueness of a component that works both in runtime and design time. Also it must persists after I close delphi; e.g combobox list values.
Thanks in advance!
below is the code, that is not working:
type
TMDBGrid = class(TDBGrid)
private
FUniqueName: String;
protected
function DefaultUniqueName: String;
function GetUniqueName: String;
procedure SetUniqueName(const AName: String);
public
constructor Create(AOwner: TComponent); override;
published
property UniqueName: String read GetUniqueName write SetUniqueName;
end;
procedure Register;
implementation
uses uComponentUtils;
procedure Register;
begin
RegisterComponents('MLStandard', [TMDBGrid]);
end;
{ TMDBGrid }
constructor TMDBGrid.Create(AOwner: TComponent);
begin
inherited;
FUniqueName := DefaultUniqueName;
end;
function TMDBGrid.DefaultUniqueName: String;
begin
Result := GenerateGUID(True);
end;
function TMDBGrid.GetUniqueName: String;
begin
Result := '';
end;
procedure TMDBGrid.SetUniqueName(const AName: String);
begin
FUniqueName := AName;
if FUniqueName = '' then
FUniqueName := DefaultUniqueName;
end;
function GenerateGUID(PlainText: Boolean = False): String;
var G: TGUID;
begin
CreateGUID(G);
Result:= GUIDToString(G);
if PlainText then
Result := MultiStringReplace(Result, ['{','}','[',']','-','.',' ','(',')'],
['','','','','','','','',''],
[rfReplaceAll, rfIgnoreCase]);
end;
"It's not working" means when a TDBGrid is added to any form, UNIQUENAME is empty. It should have a GUID.
Upvotes: 1
Views: 2275
Reputation: 612993
Your implementation of GetUniqueName
does not return anything. It needs to return FUniqueName
.
function TMDBGrid.GetUniqueName: String;
begin
Result := FUniqueName;
end;
Or you could delete the getter and change the property to be like so:
property UniqueName: String read FUniqueName write SetUniqueName;
Upvotes: 5