I need to move TreeViewItem.Text on left. My code causes runtime error.
constructor TVppTreeViewItem.Create(AOwner: TComponent);
var
c:TTextControl;
begin
inherited;
self.Text:='test';
self.TextObject.Align:=TAlignLayout.Left;
self.TextObject.Margins.Left:=50;
end;
How do I code it correctly?
At creation time TextObject
field of TTreeItem
(and your TVppTreeItem
) is nil
and accessing it results in AV error. You should move code that modifies TextObject
to ApplyStyle
method where TextObject
will be initialized from Style. Since it is not guaranteed that TextObject
will be valid even after applying style, you should check it for nil
before you attempt to do anything with it.
TVppTreeViewItem = class(TTreeViewItem)
protected
procedure ApplyStyle; override;
public
constructor Create(AOwner: TComponent); override;
end;
constructor TVppTreeViewItem.Create(AOwner: TComponent);
begin
inherited;
self.Text := 'test';
end;
procedure TVppTreeViewItem.ApplyStyle;
begin
inherited;
if Assigned(TextObject) then
begin
TextObject.Align := TAlignLayout.Left;
TextObject.Margins.Left := 50;
end;
end;