ListBox.CellTextPaint
From Xojo Documentation
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):
Case 3 // Is this the column to draw into?
// Draw an arrow to indicate that clicking this field will
// display a menu
g.DrawingColor = &c000000
// Points for a triangle on the right side of the cell
Var 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:
This code in MouseUp displays a simple menu:
Var row As Integer = Me.RowFromXY(x, y)
Var col As Integer = Me.ColumnFromXY(x, y)
If col = 3 Then
Me.SelectedIndex = row
Me.Selected(row) = True
Var base As New MenuItem
base.AddMenu(New MenuItem("Red"))
base.AddMenu(New MenuItem("Green"))
base.AddMenu(New MenuItem("Blue"))
base.AddMenu(New MenuItem("Black"))
base.AddMenu(New MenuItem("White"))
Var selectedMenu As MenuItem
selectedMenu = base.PopUp
If selectedMenu <> Nil Then
MessageBox("You changed the color to " + selectedMenu.Value + ".")
End If
End If