06-28-2012, 11:42 PM
I was contacted the other day asking to hand over a code snippet that trimmed a label if it was too long, making it display "...". So, I crafted a method that would do so!
Usage:
With this usage, the output displayed is Hello ... which was trimmed from the original "Hello my name is Brandon"
Pastebin version: <!-- m --><a class="postlink" href="http://pastebin.com/PPgYrNqm">http://pastebin.com/PPgYrNqm</a><!-- m -->
Code:
'This method is intended to trim a label that is too long and replace it with
'dots where there would normally be stuff
'Made by Brandon Milton, http://brandonsoft.com
Public Sub TrimLabel(ByVal label As Label, ByVal MaxCharCount As Integer)
If (label.Text.Length > MaxCharCount) Then
Dim remaining As Integer = label.Text.Length - MaxCharCount
label.Text = label.Text.Remove(MaxCharCount - 4, 4 + remaining)
label.Text = label.Text & "..."
End If
End Sub
Usage:
Code:
Dim Label As New Label
Label.Text = "Hello my name is Brandon"
TrimLabel(Label, 10) 'Trims to a max of 10
MsgBox(Label.Text)
Pastebin version: <!-- m --><a class="postlink" href="http://pastebin.com/PPgYrNqm">http://pastebin.com/PPgYrNqm</a><!-- m -->