How to embed stringlist property in custom delphi component?

I am creating my first custom Delphi component. Its basically a custom tpanel with title text and lines displayed on it.

I want to be able to add multiple lines of text using a string list.

When testing a component, I can’t get text lines to display on the panel when adding lines: NewLinesText.add ('line1 text')

However, it works when I create and populate a new list of strings at runtime, and then assign it to the control: controlPanelitem.NewLinesText = MyNewStringlist

I want to be able to add lines as follows: NewLinesText.add ('line1 text')

I am using Delphi 7 professional on WinXP. See code below.

Any help would be appreciated!

unit ControlPanelItem; interface uses SysUtils, Classes, Controls, ExtCtrls, Graphics, AdvPanel, StdCtrls, Windows,Forms,Dialogs; type tControlPanelItem = class(TAdvPanel) private fLinesText : TStrings; procedure SetLinesText(const Value: TStrings); procedure SetText; protected public constructor Create(AOwner : TComponent); override; destructor Destroy; override; published property NewLinesText : TStrings read FLinesText write SetLinesText; end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [tControlPanelItem]); end; constructor tControlPanelItem.Create(AOwner: TComponent); begin inherited; fLinesText := TStringList.Create; end; destructor tControlPanelItem.Destroy; begin fLinesText.Free; inherited; end; procedure tControlPanelItem.SetLinesText(const Value: TStrings); begin fLinesText.Assign(value); SetText; end; procedure tControlPanelItem.SetText; var count : Integer; begin for count := 0 to fLinesText.Count - 1 do ShowMessage(fLinesText.strings[count]); end; end. 

+6
delphi components tstringlist
source share
1 answer

You have to do

 procedure SetLines(Lines: TStrings); begin FLinesText.Assign(Lines); // Repaint, update or whatever you need to do. end; 

You may also need to set the OnChange FLines property (do this in the constructor of your custom control as soon as you create it). Install it on any TNofifyEvent component (private or protected, I suppose) procedure of your component. In this procedure, you can perform repainting, updating, etc. that you need.

I.e. do

 constructor TControlPanelItem.Create(AOwner: TComponent); begin inherited; FLinesText := TStringList.Create; TStringList(FLinesText).OnChange := LinesChanged; end; procedure TControlPanelItem.LinesChanged(Sender: TObject); begin // Repaint, update or whatever you need to do. end; 
+8
source share

All Articles