ListBox: different colors for existing rows

Hello, guys,

I have a ListBox with no columns to begin.

Then, I am adding columns to the ListBox via clicking a Button.

I use the following code in the CellBackgroundPain to paint the ListBox’s background

If row Mod 2 = 0 Then
g.ForeColor= &cD2FFF3
Else
g.ForeColor= &cD2EDF5
End If
g.FillRect(0, 0, g.Width, g.Height)

The code itself works fine. The only problem is that I cannot see how many rows I have added.

Is there any way to apply the code above only to the existing rows? It means that the ListBox would have white background, and when I add rows, their background would be painted with the code above?

Thank you in advance,

Val

add a check to see if row > me.lastindex . if so, you are beyond the rows you have added.

Only draw your custom background colors if the row is < the number of rows in the ListBox:

If row < Me.ListCount Then If row Mod 2 = 0 Then g.ForeColor= &cD2FFF3 Else g.ForeColor= &cD2EDF5 End If g.FillRect(0, 0, g.Width, g.Height) End If

Dear, Paul and Scott,

Thank you very much for your suggestions - they work perfectly.

One more question:

I am adding empty rows with the a button click. The cells are made editable with the following code

Me.CellType(row, column) = ListBox.TypeEditable
Me.EditCell(row, column)

To be able re-arrange the rows in the ListBox, I created a checkbox called “Sort Rows” and changed the code above to the following code

if CheckBox1.Value=False then
Me.CellType(row, column) = ListBox.TypeEditable
Me.EditCell(row, column)
else
Me.CellType(row, column) = ListBox.TypeNormal
end

Everything works so far - I can create rows, edit each cell within the listbox, and re-arrange the order of the rows when the checkbox is checked.

Is this the best way to make the rows re-arrangeable?

Thank you very much again,

Val