Multiline listbox

Hi. everybody.

First of all, i know that listbox doesn’t have the ability to set a width for each row. For example for two rows, you need to set it to 40px.

And I read that I can use the G.drawstring to draw the lines of text desired.

So thats my idea: measure the LEN of a string, for example---- a big phrase that have 150 characters including spaces.

The size of my column will have one fixed width.
and only fits, for example 50 characters per line.

The idea is to split the source string in parts of 50 characters. (in this case in 3 parts or pieces).

like this:

Select case Column

//Para las Columnas 3, 4 asignar formato de moneda

case 2

dim texto as String = me.Cell(row, column)

dim source as string = texto
dim dest() as string

while len(source) > 0                       // while there are still characters left in the string
  dest.append(left(source, 20))              // put each set of twenty characters into an array element
  source = right(source, len(source) - 20)   // and strip off the twenty that were just put into the array

'Msgbox dest(1)   67

For i As Integer = 0 to dest.Ubound
  g.drawString dest(0), x, y
  'g.drawString str(dest.Ubound), x, y
  g.DrawString chr(13) + dest(1), x, y

  return true

End Select

As you can see I use a Select Case for only draw in the column 2.

Then I think to use a FOR in order to parse through the items on the array. like: Drawstring dest(0)
drawstring chr(13)+dest(1)

When I choose the values on the array It works, but when I use the for doesn’t work. What Am I doing wrong?


P. D. I’m using the Celltextpaint event of the listbox

For greater precision, use g.StringWidth and add one word at a time until you exceed the graphics width, then back off by one and draw the string.

DrawString has an optional parameter wrapWidth:

This will print the cell value multiline (you need to set DefaultRowHeight to a larger value of course):

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x As Integer, y As Integer) As Boolean Dim value As String = Me.Cell(row, column) g.DrawString(value, 5, 12, Me.Column(column).WidthActual - 10) Return True End