Board index » delphi » A multi-colored listbox

A multi-colored listbox

Quote
schlemmer wrote:

> I'm creating an application that involves a fairly extensive list (up to 1000
> items) from an Interbase database. I need the user to be able to scroll through
> this list, and tag items as "Yes" or "No".

> Ideally, I would like the user to be able to scroll through the list and select
> an item. They could then press a button to say "Yes" to that item, and the item
> could change color (to green, for example). Or if they say "No" it could change
> to red. Is there any easy way to do this, or does anyone know of an existing
> (freeware) component that I would be able to use?

> I have heard of OwnerDraw listboxes... is this the way to do it?

> Thanks in advance...

> Ashley Bennett
> (be...@tartarus.uwa.edu.au)

> (reply to newsgroup or by email, or both :)

OwnerDraw is the way to go.  You might encounter more overhead than you would like (you'll have to draw
everything), but once you use ownerdrawn once, you'll get hooked.
 

Re:A multi-colored listbox


I'm creating an application that involves a fairly extensive list (up to 1000
items) from an Interbase database. I need the user to be able to scroll through
this list, and tag items as "Yes" or "No".

Ideally, I would like the user to be able to scroll through the list and select
an item. They could then press a button to say "Yes" to that item, and the item
could change color (to green, for example). Or if they say "No" it could change
to red. Is there any easy way to do this, or does anyone know of an existing
(freeware) component that I would be able to use?

I have heard of OwnerDraw listboxes... is this the way to do it?

Thanks in advance...

Ashley Bennett
(be...@tartarus.uwa.edu.au)

(reply to newsgroup or by email, or both :)

Re:A multi-colored listbox


On 12 May 1996 01:58:12 GMT, schlem@home (schlemmer) wrote:

Quote
>I'm creating an application that involves a fairly extensive list (up to 1000
>items) from an Interbase database. I need the user to be able to scroll through
>this list, and tag items as "Yes" or "No".

>Ideally, I would like the user to be able to scroll through the list and select
>an item. They could then press a button to say "Yes" to that item, and the item
>could change color (to green, for example). Or if they say "No" it could change
>to red. Is there any easy way to do this, or does anyone know of an existing
>(freeware) component that I would be able to use?

>I have heard of OwnerDraw listboxes... is this the way to do it?

YES

Two things, every listbox entry can have an associated object. You can
use this feature or just another Stringlist to store the color. About
the only trap with using objects is being sure to destroy them.
Associated objects are not automatically destroyed when you remove
items from or free the listbox.

Second, use the list box canvas.font.color and canvas.textout rather
than the listbox functions in your owner draw proceedure. If you
change the listbox font color it triggers a repaint of the entire list
to the new color. Using the canvas avoids this.

gerry

Re:A multi-colored listbox


Quote
gerry.g wrote:

> On 12 May 1996 01:58:12 GMT, schlem@home (schlemmer) wrote:

> >I'm creating an application that involves a fairly extensive list (up to 1000
> >items) from an Interbase database. I need the user to be able to scroll through
> >this list, and tag items as "Yes" or "No".

> >Ideally, I would like the user to be able to scroll through the list and select
> >an item. They could then press a button to say "Yes" to that item, and the item
> >could change color (to green, for example). Or if they say "No" it could change
> >to red. Is there any easy way to do this, or does anyone know of an existing
> >(freeware) component that I would be able to use?

> >I have heard of OwnerDraw listboxes... is this the way to do it?

Alternatively,

Have you tried setting MultiSelect to true and ExtendedSelect to False.
This combination "Keyboard Enables" the list box and the user can press
the spacebar (or use the mouse) to select an Item with out deselecting
the rest of the list. Play with the background and highlite colors and
you are up & running, without ownerdraw.

There is also a PickList freeware component that puts an X in the left
column of the list. I added a glyph property to mine that uses a
"Quicken-like" check mark.

Cheers,

Matthew

--

==================================================================
Matthew McDermott
McDermott Enterprises
matt...@valleynet.com
My Web Page: http://www.valleynet.com/~matthew
------------------------------------------------------------------
Director of Information Services & Adjunct Professor - Mathematics
The American College of Prehospital Medicine: http://www.acpm.edu/
==================================================================

Re:A multi-colored listbox


I'm sorry to sound like such an idiot, but how can I learn more about
what is, and how to use the "OwnerDraw" feature?  I understand the
concept a little, but where can I read about this?  I have "Using
Delphi" and "Delphi Unleased" but have not made it to this point.

Any suggestions??

-bk

Quote
schlem@home (schlemmer) wrote:
>I'm creating an application that involves a fairly extensive list (up to 1000
>items) from an Interbase database. I need the user to be able to scroll through
>this list, and tag items as "Yes" or "No".
>Ideally, I would like the user to be able to scroll through the list and select
>an item. They could then press a button to say "Yes" to that item, and the item
>could change color (to green, for example). Or if they say "No" it could change
>to red. Is there any easy way to do this, or does anyone know of an existing
>(freeware) component that I would be able to use?
>I have heard of OwnerDraw listboxes... is this the way to do it?
>Thanks in advance...
>Ashley Bennett
>(be...@tartarus.uwa.edu.au)
>(reply to newsgroup or by email, or both :)

Re:A multi-colored listbox


Owner-draw is simple to implement if all you want to do is
change the colours. Here are snippets from an app of mine
(which happens to use the freeware TPickList mentioned in another
reply).

{procedure to draw one text item in the list}
procedure TdlgOptions.lbCmdListDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
  with Control as TPickList do
    begin
      SetItemColors ( Canvas,
                      TCmdObj(Items.Objects[Index]).CmdSpec.IsInternal,
                      odSelected in State );
      Canvas.FillRect( Rect );
      Canvas.TextOut(Rect.Left + 4, Rect.Top, Items[Index])
    end;
end;

{procedure to set the colour according to the state of a
field in a record in the object associated with this item

This sets colors according to whether Internal is true or
false, and inverts text and background colors according
to whether the item is selected or not. }

procedure SetItemColors  ( Canvas: TCanvas;
                           Internal, Selected: boolean );
begin
    if Internal then
      begin
        if Selected then
          begin
            Canvas.Font.Color := clMenu;
            Canvas.Brush.Color := clInternalCommand;
          end
        else
          begin
            Canvas.Font.Color := clInternalCommand;
            Canvas.Brush.Color := clMenu;
          end;
      end
    else { external command }
      begin
        if Selected then
          begin
            Canvas.Font.Color := clMenu;
            Canvas.Brush.COlor := clExternalCommand;
          end
        else
          begin
            Canvas.Font.Color := clExternalCommand;
            Canvas.Brush.Color := clMenu;
          end;
      end;
  end;

Hope this helps.

BTW, with 1000 items in a list, your users may thank you if
you use a component with an incremental search like the Search
dialog in Winhelp. There are some freeware and shareware ones
around.

--
Best wishes

John Nurick

e-mail: j.nur...@dial.pipex.com

Re:A multi-colored listbox


In article <4ne5dj$...@homer.alpha.net>, an173...@anon.penet.fi says...

Quote

>I'm sorry to sound like such an idiot, but how can I learn more about
>what is, and how to use the "OwnerDraw" feature?  I understand the
>concept a little, but where can I read about this?  I have "Using
>Delphi" and "Delphi Unleased" but have not made it to this point.

There's lots of good stuff, in the on-line help, under "owner-draw controls"

Re:A multi-colored listbox


In article <4ne5dj$...@homer.alpha.net>, an173...@anon.penet.fi says...

Quote

>I'm sorry to sound like such an idiot, but how can I learn more about
>what is, and how to use the "OwnerDraw" feature?  I understand the
>concept a little, but where can I read about this?  I have "Using
>Delphi" and "Delphi Unleased" but have not made it to this point.

>There's lots of good stuff, in the on-line help, under "owner-draw controls"

Be aware, however, that in the case of an owner-drawn TOutline, the Index value
passed is the 0 based visible "row" index, not the index to the items array.  This
is a "gotcha" that got me on my first attempt and isn't covered very well in
the documentation.

Other Threads