Listbox HeaderBackgroundPaint, how to use

Hi

I´m trying to use this event but can´t figure it out.
There are two parameters, graphics and column. But how do I use the column integer?

If colum = 0 then

Or

Select case column

Case 0

Case 1

End Select

P.S. if you have a subscription to xDev, I’m having a look at the new Listbox in there …

Hi @John_Mertens_Pallesen

Take a look at this tutorial in video, I hope it helps!

Thanks, for the video. It highlighted what I was doing wrong or should I say missing to do.
I miss the g.fillrectangle method. Which I use on the cell background paint.

Damn. I need a beer after this.

3 Likes

I tried to change the color of the first column in a listbox. Based on the video, I figured out to add this piece of code:

HeaderBackgroundPaint(g As Graphics, column As Integer) As Boolean
select case column
case 1
g.DrawingColor = Color.Red
g.FillRectangle(0, 0, g.Width, g.Height)
End Select
Return True

Now ALL headers are coloured red… What is going wrong?

Put the Return True in the Case section of the Select Statement so that only that column will be affected.

select case column
case 1
g.DrawingColor = Color.Red
g.FillRectangle(0, 0, g.Width, g.Height)
Return True
End Select

Thank you, Dale… However, no change! ALL Headers are still coloured red…

Works as expected here:

I tried this small example with the same result!


In your code, you’re setting column to 1 at the very start of the event handler. That means that all will be painted red.

Remove the column = 1 line.

Thank you so much, Anthony! This is correct… I added this line (column = 1) to have a value for the column variable before the select case check. But I still do not understand why the statement column = 1 results in colouring all headers? Can you explain?

The column parameter is populated with the column index that is being drawn already. If you set it to a value, like 1, you override the value that is supplied by the framework. This event will fire for each column header as they are drawn, and the column parameter will be different for each.

You already have a value for the column variable.

The HeaderBackgroundPaint event is called for EVERY cell in the header, and when a cell calls the event it passes its column and row value to the event.

Look above your code. The HeaderBackgroundPaint event definition passes the column value into the method for EVERY header cell.

So for cell 1 of the header 1 is passed in, for cell 2 of the header 2 is passed in, etc

The select case statement allows you to set code that runs for specific columns ONLY (if column is 1 then do this, if column is 2 then do that, …).

What you do is reset each passed column value back to 1 so the select case statement for the value 1 will ALWAYS run.

in other words if you change the column variable to other value does not meant that you can paint to a different column. you can only paint in the given graphics context. as mentioned above this method is called for each column header.

Thank you all for the great explanation!