How to impliment a stringlist property in a custom delphi component?
Asked Answered
A

1

7

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

I want to be able to add multiple lines text using a stringlist.

When testing the component I cannot get the text lines to display on the panel when adding lines: NewLinesText.add('line1 text')

It does however work when I create and populate a new stringlist at runtime and then assign it to my control : controlPanelitem.NewLinesText = MyNewStringlist

I want to be able to add lines like this: 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.

Archaize answered 29/10, 2010 at 9:25 Comment(0)
A
10

You should 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 property of the FLines (do this in the constructor of your custom control, as soon as you have created it). Set it to any TNofifyEvent-compatible (private or protected, I guess) procedure of your component. In this procedure, you can do the repainting, updating etc. you need.

That is, 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;
Apollonius answered 29/10, 2010 at 9:28 Comment(7)
I am already doing that. See procedure tControlPanelItem.SetLinesText that calls SetText. (procedure SetText is not complete. Im just using showmessage to see if it is working)Archaize
OK, I didn't see that. (As you know, the code wasn't pretty a few minutes ago!) But I cannot see any OnChange?Apollonius
Hi. Thank you for your quick response. FLinesText is a stringlist and does not have an onchange property as far as I know?Archaize
@Delphiguy: Yes, it has: docwiki.embarcadero.com/VCL/en/Classes.TStringList_Events. But since the variable is declared as a TStrings (which does not have this event), you need to tell the compiler/IDE that it is a TStringList explicitly. See my update.Apollonius
This is confusing. FLinesText is declared as TStrings that does not have a onchange property, however, FLinesText is created as a TStringList that does have the onchange event. Should I type cast?Archaize
@Delphiguy: Yes, I saw that before you even posted your comment. See my update.Apollonius
@Andreas: Thank you so much! that worked perfectly! Been struggling with that the whole day!Archaize

© 2022 - 2024 — McMap. All rights reserved.