Recent

Author Topic: Setting the ListBox background color  (Read 3958 times)

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
Setting the ListBox background color
« on: February 26, 2020, 10:21:09 pm »
With the code below I'm trying to set the background color of the sole ListBox in the form, but it does not work as expected. It keeps the default property value. What am I missing?

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormCreate(Sender: TObject);
  2. Begin
  3.   { Populates the ListBox }
  4.   Form1.Color:=clSkyBlue; {works OK}
  5.   Form1.ListBox.ParentColor:=False;
  6.   Form1.ListBox.Color:=clMoneyGreen; {doesn't work, why?}
  7. end;                                                                  
  8.  
  9. { Code repopulates ListBox }

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Setting the ListBox background color
« Reply #1 on: February 26, 2020, 10:27:50 pm »
If you are in windows that is one of the controls bound to the OS themes..

 use OwnerDraw style instead where you can then specify your colors and looks at you wish..
The only true wisdom is knowing you know nothing

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
Re: Setting the ListBox background color
« Reply #2 on: February 26, 2020, 10:50:08 pm »
I am on Debian 9 with Lazarus 2.0.2 / FPC 3.0.4

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Setting the ListBox background color
« Reply #3 on: February 26, 2020, 11:09:16 pm »
I am on Debian 9 with Lazarus 2.0.2 / FPC 3.0.4

Which DE and widgetset?

For my environment, it works both in IDE's designer and when set in code.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
Re: Setting the ListBox background color
« Reply #4 on: February 27, 2020, 06:45:42 pm »
jamie,
on win8/Laz1.8/FPC3.0.4 the same test project works fine without adding anything. It's only on Debian 9 that I have a problem, i.e. I can't control the ListBox background color neither at design time nor at run time.
On debian 9 I've tried to add
Form1.ListBox.Style:=lbOwnerDrawFixed;
or
Form1.ListBox.Style:=lbOwnerDrawVariable;
but to no avail.

sash,
my configuration is
Lazarus IDE 2.0.2
FPC 3.0.4
x86_64-linux-gtk2


Is this a bug?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Setting the ListBox background color
« Reply #5 on: February 27, 2020, 08:04:24 pm »
Hi!

This seems to mbe a problem with Debian and themes.

The workaround goes this way:

* Set in the Object Inspector (OI) the Style to lbOwnerDrawFixed
* Add to your uses the LCLtype
* Connect the event OnDrawItem with the following procedure:


Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  2.  ARect: TRect; State: TOwnerDrawState);
  3. const
  4. Ceven = clAqua;
  5. Codd = clLime;
  6. Cselect = clRed;
  7.  
  8. var col : TColor;
  9. begin
  10. if odd(index) then col := codd else col := ceven;
  11. if odSelected in State then col := Cselect;
  12. TListBox(Control).Canvas.Brush.color := Col;
  13. TListBox(Control).Canvas.Rectangle(Arect);
  14. TListBox(Control).Canvas.Textout (Arect.left+2,Arect.Top+2,TListBox(Control).Items[index] );
  15. end;  
  16.  

If you become blind by the nice colors you can change the constants.

Winni

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Setting the ListBox background color
« Reply #6 on: February 28, 2020, 12:27:21 am »
@winni:
I fear OwnerDrawItem just works as it named: it paints each item (string) after whole box background rectangle was painted. So empty space (in case of number of items is not enough to fill entire box height) will not be covered by your code at all.

@MaxCuriosus:
Debian 9 is an old release, not to mention Debian are usually (significally) lagging to introduce newer software versions.
So even current version (10) doesn't have latest libraries, while ver. 9 doesn't receive any updates (except critical security fixes). So you'd better upgrade. And as I said - it works for Ubuntu (which is basically a not yet released "Debian").

Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Setting the ListBox background color
« Reply #7 on: February 28, 2020, 12:45:27 am »
@winni:
I fear OwnerDrawItem just works as it named: it paints each item (string) after whole box background rectangle was painted. So empty space (in case of number of items is not enough to fill entire box height) will not be covered by your code at all.

@sash

If too less items is the problem - then there is the old (lousy) trick:

Add items with one (or more) #32 until the visible items range is reached.
Not a nice solution but helps for  crude desires.

Winni
« Last Edit: February 28, 2020, 12:54:08 am by winni »

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Setting the ListBox background color
« Reply #8 on: February 28, 2020, 01:09:54 am »
Add items with one (or more) #32 until the visible items range is reached.
Not a nice solution but helps for  crude desires.

As I said, it is working(!) on a recent GTK version. So one simply needs to upgrade 5 years old distro (or maybe switch theme).

Not to mention you have to add extra spaces, and then (list controls are made for selecting after all) you have to handle these dummy selected strings.
To much interventions in straightforward use of standard control.

My advice would be to use ListView instead, or inherit from TCustomListBox and override its Paint for the whole control rectangle.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Setting the ListBox background color
« Reply #9 on: February 28, 2020, 01:16:59 am »
@sash

Yes - you are right.

But he asked for a solution beyond the definition of a ListBox.
So I showed how to "enlarge" the definition.That's all!

Many different ways lead to Rome .....

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Setting the ListBox background color
« Reply #10 on: February 28, 2020, 12:57:32 pm »
As I said, it is working(!) on a recent GTK version. So one simply needs to upgrade 5 years old distro (or maybe switch theme).

It's working also in older versions, as shows the attached test (code as shown in the first post) made in an Ubuntu 12.04 (so eight years old by now), running a gnome-fallback session (not Unity). Which makes it all the more baffling for the unwary; old Linux hands know that one can't rely on WM/compositors behaving always the same. ;)

Re. the image:The form is set to clNavy and the list box to clYellow, in case you didn't notice :D
« Last Edit: February 28, 2020, 12:59:18 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
Re: Setting the ListBox background color
« Reply #11 on: February 28, 2020, 11:28:34 pm »
jamie,
sash,
winni,
lucamar,

all your points are well taken. And that leaves me ever more perplexed!

1) I have attached the source of this little project whose sole purpose is to test and check stuff and things (button "Choose Year"). Sorry for the non conventional use of lower/upper case in identifiers, I come from FORTRAN and Pascal in the 70s and then Turbo Pascal. Also included are two screenshot cutouts to show the anomaly and the modest objective.

2) The code compiled on Debian 9 will show the anomaly on the same O.S. and also on Tails 4.3 and Debian 10 Live.

3) Could it be that something went wrong during the installation of Lazarus/Pascal or Debian? If so, how could I check it without reinstalling everything?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Setting the ListBox background color
« Reply #12 on: February 28, 2020, 11:32:27 pm »
Hmmmm -

are  some themes at work at the installations???

Winni

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
Re: Setting the ListBox background color
« Reply #13 on: February 28, 2020, 11:47:04 pm »
meaning?

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
Re: Setting the ListBox background color
« Reply #14 on: March 01, 2020, 11:23:05 pm »
I've tried another way.

On another Debian 9 system with the latest updates I've installed Lazarus 2.0.6 and FPC 3.0.4. and compiled the test program. The executable has the same abnormal behavior.

 

TinyPortal © 2005-2018