inheritance - Lazarus. How to invoke inherited method of dynamically created instance? -
i've searched , , didn't find situation. have variable of type tcustomcontrol:
var fcontrol:tcustomcontrol;
then use rtti magic create instance of different classes-descendants of tcustomcontrol:
constructor tfgcontrol.create(theowner: tcomponent; control: string); var classofcontrol:string; class_ref: tpersistentclass; begin case control of 'formspec': classofcontrol:='tpanel'; 'label': classofcontrol:='tlabel'; 'button': classofcontrol:='tspeedbutton'; end; class_ref:=getclass(classofcontrol); fcontrol:=tcustomcontrol(class_ref.create);
so, after have fcontrol point instance of desired class. , can assign methods of (given described needed) this:
fcontrol class_ref begin parent:=parent; if control='formspec' begin width:=400; height:=300; color:=clgray; end else begin top:=20; left:=20; width:=50; height:=20; caption:=control+' - '+inttostr(parent.controlcount); font.color:=clwhite; color:=clred; end; onmousedown:=@mousedown; onmouseup:=@mouseup; onmousemove:=@mousemove; onclick:=@click;
and works great.
but want dynamically created controls use different paint methods. want newly created instances use paint method of it's ancestors, e.g if class_ref.classname "tpanel" want fcontrol tpanel; , if class_ref.classname "tlabel" want fcontrol drawn common label.
the problem fcontrol declared tcustomcontrol , if write this:
onpaint:=@paint;
i can see (substitution) works. i'm supposed put inside new paint method invoke tpanel.paint if fcontrol tpanel; , tlabel.paint if fcontrol tlabel?
regards!
with fcontrol class_ref do
this not valid type-cast, nor needed. if want set property dynamically, can use rtti instead.
onpaint:=@paint;
that work actual tcustomcontrol
-derived objects, use is
operator check that:
if fcontrol tcustomcontrol tcustomcontrol(fcontrol).onpaint:=@paint;
Comments
Post a Comment