Multiple Lines in a row (listbox)

I am looking to have the text in a cell be multiline if it cannot fit in a single line. How would I go about doing that? I am completely at a loss…

You’ll have to draw it yourself in the CellTextPaint event. Just make sure the rows are tall enough to accommodate multiple lines.

Another way to approach this would be to use the HTMLViewer and create a table. The rows would auto size to height, and auto wrap on the word.

But can you click on the cell on the table and do something?

You can probably intercept the click (MouseDown) and put up a temporary TextField for the user to modify the content. Then update everything when the user clicks away, presses , etc.

Add a ListBox to your project, set TextSize to 16, DefaultRowHeight to 40 and align to top, left, right, bottom. Add open event:
Sub Open()
me.AddRow “”
me.CellTag(me.ListCount-1,0) = “Lorem ipsum dolor sit amet, consetetur sadipscing elitr”
me.AddRow “”
me.CellTag(me.ListCount-1,0) = “sed diam nonumy eirmod tempor invidunt ut labore et”
me.AddRow “”
me.CellTag(me.ListCount-1,0) = “dolore magna aliquyam erat, sed diam voluptua”
me.AddRow “”
me.CellTag(me.ListCount-1,0) = “At vero eos et accusam et justo duo dolores”
me.AddRow “”
me.CellTag(me.ListCount-1,0) = “et ea rebum. Stet clita kasd gubergren, no sea”
End Sub

Add CellTextPaint event
Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean
dim CellString as string
CellString = me.CellTag(row,column)
g.DrawString CellString, 0, 18, g.Width
MyStringWidth = g.StringWidth(CellString)
if MyStringWidth < g.Width then
me.DefaultRowHeight = 20
else
me.DefaultRowHeight = 40
end if
End Function

Start your app. If you resize the window the cells from the ListBox will refreshed and draw the text in one or two lines.

On a side note:
Using CellTag is good if you have only one string to draw. If you want to use more than one string you could use extra cells for each string/data you want to draw.

So set the ColumnCount to 2 or higher and set the ColumnWidth to *,0,0, … to hide the extra cells.

Then read the string in the cells and draw them in the CellTextPaint:

g.DrawString me.cell(row,1), 0, 18 to draw the string from Cell one
g.DrawString me.cell(row,2), 0, 18 to draw the string from Cell two

Alternatively you can use a string array

Dim celldata() as String
celldata.append “text one”
celldata.append “text two”
celldata.append “text three”

In the CellTextPaint now use:
g.DrawString celldata(row) 0, 18

Of course make sure you do not have more cell rows in the listbox than you have celldata.ubount

Or just store the text directly to each cell. If you return true from CellTextPaint, it won’t get shown anyway.

Good idea, Kem. Now I have the CellTextPaint updated. If you resize the listbox, the cell text is drawn from one line to three, four or more lines. Also with “me.CellType(row,column) = 3” each cell can be edited.

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean
dim CellString as string
CellString = me.Cell(row,column)

g.DrawString CellString, 0, 18, g.Width

dim MyStringWidth as double
MyStringWidth = g.StringWidth(CellString)
if MyStringWidth < g.Width then
me.DefaultRowHeight = 20
else
// Calculate RowHeight
dim RowHeightFactor as integer
RowHeightFactor = MyStringWidth / g.Width
me.DefaultRowHeight = 22 * RowHeightFactor
end if

me.CellType(row,column) = 3
return true
End Function

Sub Open()
me.AddRow “Lorem ipsum dolor sit amet, consetetur sadipscing elitr”
me.AddRow “sed diam nonumy eirmod tempor invidunt ut labore et”
me.AddRow “dolore magna aliquyam erat, sed diam voluptua”
me.AddRow “At vero eos et accusam et justo duo dolores”
me.AddRow “et ea rebum. Stet clita kasd gubergren, no sea”
End Sub