如何在自定义delphi组件中实现字符串列表属性?

发布于 2024-09-29 06:55:32 字数 1526 浏览 6 评论 0原文

我正在创建我的第一个自定义 Delphi 组件。它基本上是一个自定义 Tpanel,上面显示标题和行文本。

我希望能够使用字符串列表添加多行文本。

测试组件时,添加行时我无法让文本行显示在面板上: NewLinesText.add('line1 text')

但是,当我在运行时创建并填充新的字符串列表然后将其分配给我的控件时,它确实可以工作: controlPanelitem.NewLinesText = MyNewStringlist

我希望能够添加这样的行: NewLinesText.add('line1 text')

我在 WinXP 上使用 Delphi 7 professional。请参阅下面的代码。

任何帮助将不胜感激!

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-10-06 06:55:32

您应该这样做

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

您可能还需要设置 FLinesOnChange 属性(在创建自定义控件后立即在自定义控件的构造函数中执行此操作)。将其设置为组件的任何 TNofifyEvent 兼容(我猜是私有的或受保护的)过程。在此过程中,您可以进行所需的重新绘制、更新等。

也就是说,做

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文