Listbox - Gradient Fill

Can anybody advise me how to gradient fill a listbox?

This is normally accomplished in the Paint event that does not exist in a listbox. If I use the CellBackgroundPaint event then I get a staggered fill whereas I need a gradient fill over the whole visible listbox.

Any help would be greatly appreciated.

[quote=60411:@Simon Berridge]Can anybody advise me how to gradient fill a listbox?

This is normally accomplished in the Paint event that does not exist in a listbox. If I use the CellBackgroundPaint event then I get a staggered fill whereas I need a gradient fill over the whole visible listbox.

Any help would be greatly appreciated.[/quote]

For each row you need to draw the gradient in segments in Cellbackgroundpaint …

To figure out the start position for each row do:
Dim RowStart as integer = row - me.ScrollPosition * me.RowHeight

I think you can take it from there

  • Karen

Unfortunately you’ll have to do the calculations yourself and crop the picture at the correct place for each visible row and use CellBackGroundPaint. Also I saw a listbox that Brad Hutchings created that might do what you want. https://forum.xojo.com/3868-ann-ssg-listbox-1-0/0

No need for a picture

I meant graphics. The last time I did this I drew the graphics into a buffer picture before drawing it in CellBackGroundPaint to reduce flicker.

This is not that hard using the CellBackgroundPaint Event like this:

[code]Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) As Boolean
#pragma BackgroundTasks false

dim startcolor as color = &c606060
dim endcolor as color = &cFEFFFE

dim cellTop As Integer = g.Height * row
dim cellBottom As Integer = cellTop + g.Height - 1
dim liBoHeight as Integer = me.height-1

For x as Integer = cellTop to cellBottom step 2
dim factor As Double = x/liBoHeight
g.forecolor = rgb( _
endcolor.red - (endcolor.red-startcolor.red) *factor, _
endcolor.green - (endcolor.green-startcolor.green) *factor, _
endcolor.blue - (endcolor.blue-startcolor.blue) *factor)
g.DrawLine 0,x-cellTop,g.width,x-cellTop
g.DrawLine 0,x-cellTop+1,g.width,x-cellTop+1
next
End Function
[/code]

The only drawback is the slow drawing.
And this code dos only work for Listboxes where RowHeight is the same for all rows.

That does not take into account Vertical scrolling
As I said above It should be
dim cellTop As Integer = g.Height * (row - me.ScrollPosition)

Also one need to take into account if a header is visible or not

Not sure it would be slow… It would likely be OK… But one could cache the colors in an array, and only recalculate them if the listbox height was changed to speed it up… And as Suggested above one could use a picture with the same need to recreate it if the listbox dimensions changed

Well this was only a small example how to solve this using plain Xojo code.
It is nothing more than a start and you need to adjust it for your needs.

Right. I did not test that and it looks really bad. So just chnage one line where cellTop is calculated:

  dim cellTop    As Integer = g.Height * (row-me.ScrollPosition)

Thank you Thomas and Karen.

This has resolved my problem.

i have an example LBox

[quote=60468:@Axel Schneider]i have an example LBox

[/quote]
Thank you, Axel, but that is not what I was describing. I have similar list boxes that offer a gradient fill in each line (as yours does) and I was looking for a uniform gradient fill over the whole listbox contents.

This has been supplied to me above.

Thanks again.