Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trimming a label
#1
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!

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)
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 -->
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
Trimming a label - by brandonio21_phpbb3_import2 - 06-28-2012, 11:42 PM
Re: Trimming a label - by Vinwarez - 07-10-2012, 04:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Editable Label brandonio21 2 11,543 01-04-2013, 09:30 PM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)