Click to See Complete Forum and Search --> : String truncation


bn2vs
10-15-2008, 12:14 PM
Hey,

I'm looking for a way to truncate strings after a certain length in px, so i'de be able to do something like.
str = truncate(str, 100)

Anyone a suggestion?

Ron Weller
10-15-2008, 07:06 PM
str = Left$(str, 100)

Phil Weber
10-16-2008, 02:57 AM
Ron: I think he wants to truncate the string based on a maximum pixel width, not number of characters.

bn2vs: The simplest method is to use a Label. Set the Label's width to the desired maximum; it will truncate the text automatically.

The hard way is to use the Clip and DrawString methods of the Form's Graphics object:

Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim Text As String = "This is a long string of text."
Dim MaxPixelWidth As Integer = 100

e.Graphics.Clip = New Region(New Rectangle(10, 10, MaxPixelWidth, 15))
e.Graphics.DrawString(Text, Me.Font, Brushes.Black, 10, 10)
End Sub

I don't know of any way to truncate a string to a maximum pixel width and assign the result to another string.

bn2vs
10-16-2008, 03:38 AM
Hey,

I'll use the gfx solution for the time being, but will continue to search for a better alternative.

I'll let you guys know if i find anything :)