ListBox.CellTextPaint

From Xojo Documentation

Event


ListBox.CellTextPaint(g as Graphics, Row as Integer, Column as Integer, x as Integer, y as Integer) As Boolean

Supported for all project types and targets.

The parameter g is a Graphics object that corresponds to the text drawing area of the cell identified by Row, Column. Row and Column are zero-based. This does not necessarily correspond to the entire cell content area, for example, if you use a row picture in the cell.

Notes

In order for this event handler to be called, the cell itself must have been given a value, even if it is blank.

The parameters x and y are the coordinates of the suggested ideal location to draw text based on the current value of ColumnAlignment or CellAlignment, as well as the cell's font, font size, and font style.

The drawing order of the cell is as follows, with the background first:

  • Background
  • Disclosure Triangle/Treebox
  • Checkbox
  • RowPicture
  • Text
  • Border

Although the border is painted last, it isn't advisable to change the state of the border in the CellTextPaint event since the area is determined by the size of the border before the cell is painted. It could leave unpainted areas or possibly cover up some of the painting you have done.

Returning True means the user has handled the text paint and no other processing is to be done with the text. In this case, the user is responsible for text highlighting. Text highlighting is currently only done for the hierarchical listbox style. Returning False means the user wants the default text drawing. In this case, the text will be highlighted as appropriate (according to platform) for you automatically.

Example

This code draws a small triangle on the right side of the cell (the code is using column 3 as the cell to draw into):

Select Case column
Case 3 // Is this the column to draw into?
// Draw an arrow to indicate that clicking this field will
// display a menu
g.ForeColor = &c000000

// Points for a triangle on the right side of the cell
Dim points(6) As Integer
points(1) = g.Width - 10
points(2) = 1
points(3) = g.Width
points(4) = 1
points(5) = g.Width - 5
points(6) = 10

g.FillPolygon(points)

Return True
End Select

Now you can display a menu when the user clicks in the cell by putting code in the MouseDown and MouseUp event handlers.

This goes in MouseDown to allow the MouseUp event handler to be called:

If Me.ColumnFromXY(x, y) = 3 Then
Return True
End If

This code in MouseUp displays a simple menu:

// Display menu if clicked in PopupMenu column
Dim row As Integer = Me.RowFromXY(x, y)
Dim col As Integer = Me.ColumnFromXY(x, y)

If col = 3 Then
Me.ListIndex = row
Me.Selected(row) = True

Dim base As New MenuItem
base.Append(New MenuItem("Red"))
base.Append(New MenuItem("Green"))
base.Append(New MenuItem("Blue"))
base.Append(New MenuItem("Black"))
base.Append(New MenuItem("White"))

Dim selectedMenu As MenuItem
selectedMenu = base.PopUp

If selectedMenu <> Nil Then
MsgBox("You changed the color to " + selectedMenu.Text + ".")
End If
End If