Listbox multiline per row

Listboxes can contain only one line of text per row. I found this code to trick the listbox object. The question I have is if it is possible to include typeface info for bold or colored text?

[code]By default, listboxes only draw one line of text per row. That being said, you
can change the DefaultRowHeight property and draw the text yourself in the
CellTextPaint event. The x and y parameters that are passed to the event are
where the listbox would have drawn the text itself, so the x parameter will
probably be right, whereas the y parameter will be set up to draw the text
centered vertically.

As an example, setting the DefaultRowHeight to 48 and then setting the
CellTextPaint event to:

dim str as string = me.cell(row,column)
g.DrawString str,x,y-g.TextHeight
return true

Grabs the text from the cell and draws it manually. Returning True tells the
listbox that you have already handled drawing the text for that cell.

In the Open event, do this:

me.AddRow “Mickey Mouse” + EndOfLine + “123 Main Street” + EndOfLine +
“Orlando, FL 32222”[/code]

You can control the font, etc. by changing the properties of the g As Graphics parameter to the CellTextPaint event. See: Graphics

e.g. this would set the font to Courier, bold and red:

Event CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean
  g.ForeColor = &cFF000000
  g.Bold = True
  g.TextFont = "Courier"
End Event

Thanks, but is it possible, as in my example where I have 3 lines per row, to give each line a separate typeface?

Karen Atkocius is the absolute expert when it comes to listboxes. You should pm her.

Mmm… the PM tool gives repeatedly the message ‘Can’t contact the server… wait a few moments’. I hope that Karen reads this post…

Event CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean Dim arr() As String = Split(Cell(row, column), EndOfLine) g.Bold = True g.TextFont = "Impact" g.DrawString(arr(0), x, y) g.Bold = False g.TextFont = "Courier" g.DrawString(arr(1), x, y + g.TextHeight) ... Return True End

OK… thanks… but there’s something not quite correct…

In the CellTextPaint event I have this code:

Dim arr() As String = Split(me.Cell(row, column), EndOfLine) g.Bold = True g.TextFont = "Impact" g.DrawString(arr(0), x, y) g.Bold = False g.TextFont = "Courier" g.DrawString(arr(1), x, y + g.TextHeight) return True

In the Open event of the window I put

Listbox1.AddRow("Mickey Mouse" + EndOfLine + "123 Main Street" + EndOfLine + "Orlando, FL 32222")

But upon running something goes wrong… no visible form and debugging on the Open or Paint event doesn’t work… The only thing on the Windows is this Listbox…

This is working for me with the defaultrowheight on the listbox set to 48.

  Dim arr() As String = Split(me.Cell(row, column), EndOfLine)
  g.Bold = True
  g.TextFont = "Impact"
  g.DrawString(arr(0), x, y - g.TextHeight )
  g.Bold = False
  g.TextFont = "Courier"
  g.DrawString(arr(1), x, y)
  g.Bold = False
  g.TextFont = "Courier"
  g.DrawString(arr(2), x, y + g.TextHeight)
  return True

Yep. Working! Thanks very much!