Stupid question about ListBox

I Get outofbounds and I cannot get out of it…
The ListBox have 5 Rows added in Opening. I do not even time to see my window…

I am tired, cold and hungry.

Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean
  // CellBackgroundPaint
  // 
  If Me.RowTagAt(Row) = "Section" Then
    g.DrawingColor = cmy(0,0,1)
  End If
  g.DrawRectangle 0,0,100,20
End Function

CellBackgroundPaint can happen even if there’s no row there. You can’t assume a row exists in this event. It has always been that way.

Quite right, @Tim_Parnell , one needs to check that row not = -1

Edit: you can also get -1 if the mouse is in the scrollbar.

If I recall correctly the event is fired for all possible visible rows…
So if it could display 16 rows the event is fired 16 times…

Your code stops at row = 5…so always check how many rows you have upfront (o;

@Emile_Schwarz
First of all, I want to emphasize that there are no dumb or stupid questions.
The only foolish question is the one that’s never asked. :innocent:

I thought that the CellBackgroundPaint event is raised on all visible rows. Even if some rows don’t exist.

You could wrap your code in an If…Then statement:

If row > -1 And row <= me.LastRowIndex Then
  If Me.RowTagAt(Row) = "Section" Then
    g.DrawingColor = cmy(0,0,1)
    g.DrawRectangle 0, 0, g.Width, g.Height // instead of hard-coding the size, I used the width and height provided by the graphics object "g"
    Return True // Don't forget to return with True, otherwise Xojo will override your beautiful "paint art"
  End If
End If

Well, I’m afraid I can’t help you with that…

Sorry, but what can I do ?
(too early to go to sleep). :wink:

At my age, it’s never too early to sleep :wink:

1 Like

You two are saying the same thing but with different words. You are both understanding correctly.

Yeah, “Welcome Language barrier” Haha :wink:

Something like:

If row < Me.RowCount Then
...Do you thing
End If

When I don’t understand something completely I have my ConsoleLog method handy…and there I discovered that row is incremented for all rows that could exist in the view…

May not sound clear…but this allows to also paint an alternate gray/white cell background…think there is even an official example for this…

Well, a row could be -1.
that could result in an error. So, include row > -1 as well:

If row > -1 And row < Me.RowCount Then
  // Do your thing
End If

EDIT: Actually in the CellBackgroundPaint event the row value should not be -1.
So, ignore my brain-spasm :face_with_spiral_eyes:

But in which case does row - 1 occur (just our of curiosity ;o)

For CellBackgroundPaint I don’t believe it does.

Just in case you do not noticed it, here’s from where I go:

I only wanted to add some ContextualMenus…

This removed the OOB Exception. But the background color does not change. I will explorate more.

g.DrawRectangle 0, 0, g.Width, g.Height

…will draw a hollow rectangle.

Try g.FillRectangle 0, 0, g.Width, g.Height

1 Like

When the row selection changes.

If there is no row selected, the SelectedRowIndex would be -1.
Also, the SelectedRowCount would be 0 in that case.

Another case is in some Drag events, like “DragOverRow”, when an object is dragged over the List Header.

The problem lies with the x,y click location to set the Row Tag. Apparently.

I do not clicked where the 100% yellow background appeared:

but at last, it appeared !
(I added the Tag with the Mouse, draw in CellBackgroundPaint)

When you click on a row, the SelectedRowIndex should be set, I think.
You could test if that value < 0.

If it is < 0, ignore the click event.
Otherwise, get the RowIndex from XY position.

This is CellBackgroundPaint, it passes row/column parameters, it has nothing to do with the selection. The row parameter passed will never be -1 as far as I can tell.