开发者

Publishing a property in a Lazarus component raises an 'Access violation'

开发者 https://www.devze.com 2022-12-17 20:06 出处:网络
in Lazarus on Linux, I registered a class as a component, so that I can drop it on a form. Works like a charm, except that the properties that I can publish are limited to simple types like strings a

in Lazarus on Linux, I registered a class as a component, so that I can drop it on a form.

Works like a charm, except that the properties that I can publish are limited to simple types like strings and integers.

Whenever I try to publish a property like a TStringList or a TImage, it raises an 'Access Violation' when I click on it in the object inspector.

I 开发者_运维技巧compared my code with the standard components, but I can't see what they're doing differently.

So what are the additional steps necessary to use such properties in the object inspector?


To pulish a class property you need to: 1. Create that property inside your component contructor - so it will never be nil 2. in your property Setter you need to assign from new value to your component. So you must implement Assign method or AssignTo in your component. e.g.

TMyComponent = class
private
  FString: TStrings;
published
  property Strings: TStrings read FStrings write SetStrings;
end;

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  // always create it in the constructor so it will not be nil
  FStrings := TStringList.Create;
end;

procedure TMyComponent.SetStrings(const AValue: TStrings);
begin
  // this is correct statement
  FStrings.Assign(AValue);
  // this is not correct
  // FStrings := AValue;
end;


I think this is about the same as in Delphi, for complex types to be used designtime there needs to be designtime code that handles showing/editing them.

0

精彩评论

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

关注公众号