I wrote three identical functions as shown below. Only the signatures are changed to enable all functions to live in harmony with the same name. I placed them in a Class called "StringExtensions" and declared each function as "Shared" so external coding could access them without creating an instance variable of the class.
Public Shared Function TextLengthInPixels(oTextBox As TextBox, bTrim As Boolean) As Integer
Return TextRenderer.MeasureText(IIf(bTrim, oTextBox.Text.Trim, oTextBox.Text), oTextBox.Font).Width
End Function
Public Shared Function TextLengthInPixels(oLabel As Label, bTrim As Boolean) As Integer
Return TextRenderer.MeasureText(IIf(bTrim, oLabel.Text.Trim, oLabel.Text), oLabel.Font).Width
End Function
Public Shared Function TextLengthInPixels(oRTB As RichTextBox, bTrim As Boolean) As Integer
Return TextRenderer.MeasureText(IIf(bTrim, oRTB.Text.Trim, oRTB.Text), oRTB.Font).Width
End Function
Just use whichever one you want (or write more for other controls which have a "text" property).