BP Forums
Trimming a label - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=18)
+----- Thread: Trimming a label (/showthread.php?tid=549)



Trimming a label - brandonio21 - 06-28-2012

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 -->


Re: Trimming a label - Vinwarez - 07-10-2012

I just stumbled upon this post and when I saw the code, it reminded me on something.

Quote:Public Sub TrimLabel(ByVal label As Label, ByVal MaxCharCount As Integer)
Does "ByVal" means decalring a parameter for method? Like in Java?

Would it be something like this in Java?
Code:
public void TrimLabel(label Label, int MaxCharCount){
//code
}
(Even though I am not sure if type label exists. I assume it would be JLabel or something.)


Re: Trimming a label - brandonio21 - 07-10-2012

Vinwarez Wrote:Does "ByVal" means decalring a parameter for method? Like in Java?

This is exactly right! Of course, I don't think that ByVal is completely necessary in VB.NET. For example, I think the code snippet
Code:
Public Sub TrimLabel(label As Label,MaxCharCount As Integer)
Would also work.


But yes, you are right!