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.
#2
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.)
Also known as Rocketalypse.
System.out.println("I prefer Java.");
#3
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!
My Blog | My Setup | My Videos | Have a wonderful day.


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

Forum Jump:


Users browsing this thread: 1 Guest(s)