Limit textwidth in a cell

I have a listbox-Cell, with a file path in it, that I can either
change by hand
or
change pressing a 'Browse… button" that I draw on the right hand side of this (custom-) cell.

Everything works fine except that the text overflows the button.
How can I limit the width of a text in a text cell (and at the same time keep the text editable)?

Thanks in advance

One possibility is to draw the text yourself in the CellTextPaint event. Use Graphics.Clip to create a Graphics object representing the area within the cell you want to have text in, then draw the text.

e.g.

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean Dim area As Graphics = g.Clip(0, 0, g.Width / 2, g.Height) ' limit to half the width of the cell area.DrawString(Me.Cell(row, column), x, y) Return True End Function

[quote=407865:@Andrew Lambert]One possibility is to draw the text yourself in the CellTextPaint event. Use Graphics.Clip to create a Graphics object representing the area within the cell you want to have text in, then draw the text.

e.g.

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean Dim area As Graphics = g.Clip(0, 0, g.Width / 2, g.Height) ' limit to half the width of the cell area.DrawString(Me.Cell(row, column), x, y) Return True End Function [/quote]

I tried it that way but then the text besoms static and if I use the regular cell(row, column) to enter some cell content it still runs over my button. I could draw the browse button on the left side and use a text offset but that just doesn’t look right.

When calling drawstring, try setting the width parameter and condense parameters. This will draw the text with an ellipsis if it gets too long.

Just make sure you return True from CellTextPaint so it doesn’t draw the text again.

All this time I was reading “CellBackgroundPaint” while you guys wrote “CellTextPaint”
Need to get my eyes checked (and my brains)

Thanks everything is working now :slight_smile: