delphi - How can I make a TList property from my custom control streamable? -
overview
i writing own listbox control derived tcustomlistbox
.
i have began implementing own property editor allowing editing caption , imageindex of items @ designtime (i custom drawing listbox , have published own imagelist property control).
problem
so far working well, cannot items stream dfm. believe due fact standard listbox items
property of tstrings
, , control have published own property named items
, of tlist
class. dont want standard items
property of listbox appear instead own items
type.
so though can drop control onto form @ designtime, edit items
own property editor, if run application listboxes empty, if view dfm @ designtime , return form view items gone, not been stored in dfm , unsure how correct this?
relevant extracts of code looks (comments added purpose of question):
my own listbox item type:
tlistboxitem = class(tobject) // tried tpersistent private fcaption: string; fimageindex: integer; public // tried published property caption: string read fcaption write fcaption; property imageindex: integer read fimageindex write fimageindex; end;
the custom control:
tmylistbox = class(tcustomlistbox) private fimagelist: timagelist; fitems: tlist; procedure setimagelist(const value: timagelist); protected procedure notification(acomponent: tcomponent; operation: toperation); override; procedure drawitem(index: integer; rect: trect; state: townerdrawstate); override; procedure measureitem(index: integer; var height: integer); override; public constructor create(aowner: tcomponent); override; destructor destroy; override; procedure additem(acaption: string; aimageindex: integer); // adds new item fitems , listbox control published ..... property color; property images: timagelist read fimagelist write setimagelist; property items: tlist read fitems write fitems; // take on standard items type , publish own items type instead ..... property sorted; property taborder; property tabstop; property visible; property onclick; property oncontextpopup; property ondblclick; property ondragdrop; property ondragover; //property ondrawitem; property onenddock; property onenddrag; property onenter; property onexit; property onkeydown; property onkeypress; property onkeyup; //property onmeasureitem; property onmousedown; property onmouseenter; property onmouseleave; property onmousemove; property onmouseup; ..... end;
that control in simple form. want control such custom drawing , property editor items
property etc working how want.
question
what need in order make fitems
property streamable dfm? dfm should show each of own listbox item type under items
.
thanks.
there 2 possibilities:
1) use tcollection
instead of tlist
, item class should descendant of tcollectionitem
. that's how panels
property implemented in tstatusbar
, example. way preferable if items can made of same class , if can descendants of tcollectionitem
tpersistent
descendant. looks that:
tmylistboxitem = class (tcollectionitem) private fcaption: string; fimageindex: integer; published //only published properties saved dfm property caption: string read fcaption write fcaption; property imageindex: integer read fimageindex write fimageindex; end; tmylistboxitemclass = class of tmylistboxitem; //class reference, or metaclass, need initialize tcollection tmylistbox = class(tcustomlistbox) private // imagelist etc... fitems: tcollection; public constructor create(aowner: tcomponent); override; destructor destroy; override; // other funcs published property items: tcollection read fitems write fitems; //other properties end; //implementation constructor tmylistbox.create(aowner: tcomponent) begin inherited; //other initializations fitems:=tcollection.create(tmylistboxitemclass); //so tcollection able create items many needed end; destructor tmylistbox.destroy; begin fitems.free; //other destruction inherited destroy; end;
that's it. way, never descend tobject
when need streaming dfm, begins tpersistent
(which means ability saved , loaded) , implemented in tcomponent
.
2) each item tcontrol
descendant has custom list box parent , form owner (possibly tcomponent
suffice). that's how it's done in ttabcontrol
, tpagecontrol
, tmainmenu
, other components. in case items might contain own items, too, structure shown in 'structure' panel in ide, quite handy. in case can't have 'items' property in object inspector, instead you'll have implement own buttons in popup menu @ design time, 'items editor' or 'new item' etc. implementation useful if items can 'draw themselves', of different types , possibly have own children.
Comments
Post a Comment