Board index » delphi » Help - Need enhancement to TCustomlistbox

Help - Need enhancement to TCustomlistbox

I need a listbox where I can set the font attributes differently for each
item in the list. Ideally I'd also like to store 'other' information again
for each item, but that is not displayed on the control.

Any helpful ideas appreciated.

John.

 

Re:Help - Need enhancement to TCustomlistbox


In article <3985c92...@news1.vip.uk.com>, "john.greening"

Quote
<john.green...@tinyonline.co.uk> writes:
>I need a listbox where I can set the font attributes differently for each
>item in the list.

You don't really need another listbox. Essentially the OwnerDraw style enables
you to do this. You select the font in that event handler appropriately, maybe
using a set to hold the indices of the items of any one font and then set that
font for the canvas.

Declare the sets containing appropriate index values :-

type
  TIndexSet = set of byte;

const
  {indices of listbox items for corresponding attributes}
  ArialIndices : TIndexSet = [0, 2, 3, 6];
  CourierIndices : TIndexSet = [1, 4, 5];
  LargeIndices : TIndexSet = [0, 4, 6];
  BoldIndices : TIndexSet = [1, 3];
  RedIndices : TIndexSet = [0, 1, 5];

Set the TListBox.Style to lbOwnerDrawFixed

Then code the OnDrawItem event handler :-

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with TListBox(Control).Canvas do begin
    {set font attributes as appropriate}
    if Index in ArialIndices then
      Font.Name := 'Arial';
    if Index in CourierIndices then
      Font.Name := 'Courier New';
    if Index in LargeIndices then
      Font.Size := 12;
    if Index in RedIndices then
      Font.Color := clRed;
    if Index in BoldIndices then
      Font.Style := [fsBold]; // Font.Style is a set - hence in square brackets
    {draw the text}
    TextOut(Rect.Left, Rect.Top, TListBox(Control).Items[Index]);
  end;
end;

 Ideally I'd also like to store 'other' information again

Quote
>for each item, but that is not displayed on the control.

Every listbox also has an Objects array in which you can store any DWord (ie
integer) value. This is typed as a TObject and can be one, or it can be
typecast to any integer or Dword value. So it can be a pointer to a string (or
PChar), but you have to allocate the memory before use, and free it after use.

To store an integer

  ListBox1.Items.AddObject('This Item', TObject(12345));

or to add or change a stored integer :-

  ListBox1.Items.Objects[3] := 765432;

. . . and to retrieve it :-

  MyInteger := integer(ListBox1.Items.Objects[3]);

To store an object (eg a TForm) :-

  ListBox1.Items.Objects[3] :=Form2; // no type-cast needed - it is a TObject
descendant

.. and use it in the OnClick event handler :-

  With ListBox1 do
    if ItemIndex <> nil then
      TForm(Items[ItemIndex]).ShowModal;  // type-cast it to a TForm

To store a colour which you might use for colouring the text :-

  ListBox1.Items.Objects[2] := TObject(clRed);

. . . and to retrieve it :-

  MyColor := TColor(ListBox1.Items.Objects[2]);

Alan Lloyd
alangll...@aol.com

Re:Help - Need enhancement to TCustomlistbox


In article <3985c92...@news1.vip.uk.com>, john.green...@tinyonline.co.uk
says...
Quote
> Any helpful ideas appreciated.

To Alan's excellent answer, I would only add that if your fonts are
substantially different from each other, you may want to set the Style to
lbOwnerDrawVariable and respond to OnMeasureItem, to set the height of
each item. You can use the API function DrawText() with the DT_CALCRECT
flag to calculate the smallest rectangle that will fit the text, then add
a few pixels to the height.
--
Neil J. Rubenking
Contributing Technical Editor
PC Magazine

Re:Help - Need enhancement to TCustomlistbox


thanks guys, this is most appreciated. I'm still learning and this type of
reply aids my understanding significantly.

John.

Re:Help - Need enhancement to TCustomlistbox


Quote
In article <3985c92...@news1.vip.uk.com>, John.greening wrote:
> I need a listbox where I can set the font attributes differently for each
> item in the list. Ideally I'd also like to store 'other' information again
> for each item, but that is not displayed on the control.

Just to follow up an Alan's posting, you could for simplicity combine both.  
E.g., if you put in a given item "item 5\this is some extra information" you
could arrange to paint only the "item 5" and the rest could be anything you
like.  This avoids goofing around with the Objects[] array if you'd prefer not
to.

--
Tue, 01 Aug 2000 12:24 EDT
Jim O'Brien, UnitOOPS Software   unito...@remove-this-prefix.unitoops.com
Check out our OLE Drag and Drop Components at <http://www.unitoops.com/>
Components for Delphi 5 included *free* on the D5 Companion CD.
Browse examples online at <http://www.unitoops.com/uoole/examples/>

Re:Help - Need enhancement to TCustomlistbox


In article <VA.0000001e.051d7...@remove-this-prefix.unitoops.com>,
unito...@remove-this-prefix.unitoops.com says...
Quote
> This avoids goofing around with the Objects[] array if you'd prefer not
> to.

But... but... but... "goofing around" with the Objects array is an
excellent activity! It's incredibly convenient, being able to create a
tiny TObject descendant to hold any information at all that's associated
with the listbox items. Ditto with the Data member of treeview and
listview nodes. I just had to speak up in favor of those handy features.
--
Neil J. Rubenking
Contributing Technical Editor
PC Magazine

Re:Help - Need enhancement to TCustomlistbox


"Neil J. Rubenking" <jrube...@SPAM-B-GONE.mother.com> wrote in message
news:MPG.13f1c1fec2d371e798992b@news.mother.com...

Quote
> In article <VA.0000001e.051d7...@remove-this-prefix.unitoops.com>,
> unito...@remove-this-prefix.unitoops.com says...
> > This avoids goofing around with the Objects[] array if you'd prefer not
> > to.

> But... but... but... "goofing around" with the Objects array is an
> excellent activity! It's incredibly convenient, being able to create a
> tiny TObject descendant to hold any information at all that's associated
> with the listbox items. Ditto with the Data member of treeview and
> listview nodes. I just had to speak up in favor of those handy features.

Let me add my vote in favor of the Objects array. It is useful for
associating objects with entries and also quite handy for storing any 4 byte
value that relates to entries, including record pointers.

Re:Help - Need enhancement to TCustomlistbox


In article <MPG.13f1c1fec2d371e7989...@news.mother.com>, Neil J. Rubenking
wrote:
Quote
> But... but... but... "goofing around" with the Objects array is an
> excellent activity! It's incredibly convenient, being able to create a
> tiny TObject descendant to hold any information at all that's associated
> with the listbox items. Ditto with the Data member of treeview and
> listview nodes. I just had to speak up in favor of those handy features.

Neil:

I agree with you - it's a great facility.  However, it does impose a
requirement for some pretty strict discipline with memory management of the
TObject descendants you put in the array.  The suggestion I made is nice for
quick-and-dirty purposes, since you let Delphi's normal string handling take
care of all the memory management issues for you.  (I did say "if you'd prefer
not to..." <g>)

Regards, Jim.

P.S.  It's been a long time - how are you doing?

--
Thu, 03 Aug 2000 09:32 EDT
Jim O'Brien, UnitOOPS Software   unito...@remove-this-prefix.unitoops.com
Check out our OLE Drag and Drop Components at <http://www.unitoops.com/>
Components for Delphi 5 included *free* on the D5 Companion CD.
Browse examples online at <http://www.unitoops.com/uoole/examples/>

Other Threads