Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Inheritance

Status
Not open for further replies.

rcloutie

Programmer
May 26, 2005
164
CA
Hello,

I'm building a VCL based on TListBox control. I want to setup a brand new property called CheckBox (Boolean) which will flip from TListBox based inheritance to TCheckListBox based inheritance.

TNewList = class(TListBox);
turns to
TNewList = class(TCheckListBox);

Is it possible to do so?

Thanks
 
No way. You'd be better off setting up your component as a TCheckListBox, and modifying the drawing behaviour so that your Checkbox property influences what the list looks like.
 
Ok no dynamic inheritance. No problem to set up component as TCheckListBox. Now, how to modify the drawing behavior (hide the CheckBox section within the ListBox)?

Thanks
 
Set the Style property to lbOwnerDrawFixed. Then attach to the OnDrawItem event. You then write code in your custom DrawItem method to draw each item the way you want.

Alternatively, you may want to use a TPageControl instead, set up two pages, one with a TListBox, the other with a TCheckListBox, hide the TPageControl tabs and simply flip between the two pages as needed.
 
Ok, and any idea of the code to write within OnDrawItem?
 
sure. start with this code block:

Code:
procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer Rect: TRect; State: TOwnerDrawState);
begin
  with TCheckListBox(Control).Canvas do
  begin
    // do stuff here to draw on the canvas
  end;
end;

Use the help file to find out how to use the Canvas property. The first one you should check out is the TextOut method. You can change colours, font styles, and you can draw lines and shapes on the canvas. Daunting at first, but fun

This OnDrawItem method gets called each time an item needs to be drawn. Use the variables passed to the OnDrawItem method. Index will be the Item index that is to be drawn, and Rect will be a TRect variable of the space on the canvas that this item can occupy. Use the help file to see what TOwnerDrawState is about.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top