2010-10-29 109 views
6

我正在創建我的第一個自定義的Delphi組件。它基本上是一個自定義Tpanel,其上顯示標題和行文字。如何在自定義的delphi組件中實現stringlist屬性?

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

當測試組件,我不能得到文本行添加行時在面板上顯示:NewLinesText.add(「一號線文」)

但它確實在工作的時候創建並填充在運行一個新的StringList然後將其分配給我的控制:controlPanelitem.NewLinesText = MyNewStringlist

我希望能夠增加線路是這樣的:NewLinesText.add(「一號線文」)

我使用在WinXP德爾福7個專業。見下面的代碼。

任何幫助,將不勝感激!

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.

回答

8

你應該做

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; 
+0

我已經這樣做。請參閱調用SetText的過程tControlPanelItem.SetLinesText。 (程序SetText不完整,我只是使用showmessage來查看它是否工作) – 2010-10-29 09:33:51

+0

好的,我沒有看到。 (正如你所知,代碼在幾分鐘前還不算漂亮!)但是我看不到任何'OnChange'? – 2010-10-29 09:35:39

+0

嗨。感謝你的快速回復。據我所知,FLinesText是一個字符串列表,並且沒有onchange屬性? – 2010-10-29 09:39:05