如何在自定义delphi组件中实现字符串列表属性?
我正在创建我的第一个自定义 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该这样做
您可能还需要设置
FLines
的OnChange
属性(在创建自定义控件后立即在自定义控件的构造函数中执行此操作)。将其设置为组件的任何 TNofifyEvent 兼容(我猜是私有的或受保护的)过程。在此过程中,您可以进行所需的重新绘制、更新等。也就是说,做
You should do
You may also need to set the
OnChange
property of theFLines
(do this in the constructor of your custom control, as soon as you have created it). Set it to anyTNofifyEvent
-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