Sunday, April 22, 2012

Measuring the length of each line of a string

I needed to have the ability to measure the length of each line of a string that would be displayed in a text box using vb.net.

This is what it does: You tell it the string that you want to use, the text control that it is going to use and the number of rows you want to display. The function builds line-by-line the string until no more will fit and returns the new string that will fit within that control.


    Private Function fitTextToLength(stringToFit As String, textfield As Object, rows As Short)
        ' This function is very important for displaying text in the alerts properly.
        ' stringToFit is the string that is going to be used to populate the control
        ' textField is the object where teh text will be placed. We will have to consider the size
        '           of the field for this function.
        ' rows indicated how many rows is wanted for the field. Multiline fields would be more then 1
        '           depending on useage requirements
        ' Split the words
        Dim words() As String = stringToFit.Split(" ")
        Dim complete As Boolean = False
        Dim counter As Short = 0
        Dim row As Short = 0
        Dim stringLine As String = String.Empty
        Dim finalText As String = String.Empty
        ' Loop until we find a complete string
        While Not (complete)
            ' Build a string to mesaure
            ' This is built word-by-word until the string is too long for the objects width
            ' Each word is not added to the string until it is known that it will fit.
            Dim myString As String = stringLine & words(counter) & " "
            ' Measure the string length for comparision
            Dim g As Graphics = Graphics.FromHwnd(textfield.Handle)
            Dim f As SizeF = g.MeasureString(myString & "...", textfield.Font)
            ' Determine if the built string fits the width of the object
            If f.Width <= textfield.width Then
                ' Concatenate the string to the main string
                stringLine = myString
                counter += 1
            Else
                ' Check to see if we have reach the max row
                If row = rows Then
                    ' Show that there is more text using "..."
                    finalText += "..."
                    ' Complete the loop
                    complete = True
                Else
                    ' Increase the row 
                    row += 1
                    ' Concat the final text
                    finalText += stringLine
                    ' Clear the stringLine for the next line to build
                    stringLine = String.Empty
                End If
            End If
            ' Make sure that we don't go beyond the array
            If counter >= words.Length Then
                ' Concat the build string to the main string
                finalText += myString
                ' Exit the loop
                complete = True
            End If
        End While
        ' Return the result.
        Return finalText
    End Function