Results 1 to 14 of 14

Thread: [RESOLVED] [2008] Adding "..." to long text

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Resolved [RESOLVED] [2008] Adding "..." to long text

    I have a problem with some of the files i work with, the names are too long when they show up in the label and i want to add "..." to the shorten the text. So lets say after 5 letters it add "..."
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: [2008] Adding "..." to long text

    This should work...

    vb Code:
    1. Dim filename as String
    2.   Dim truncatedName As String
    3.  
    4.   If filename.Length > 5 Then
    5.       truncatedName = filename.Substring(0, 5) + "..."
    6.    End If
    Last edited by wy125; Oct 22nd, 2008 at 03:38 PM.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [2008] Adding "..." to long text

    try this:

    vb Code:
    1. Private Sub update_label(ByVal newDir As String)
    2.      Dim gr As Graphics = Graphics.FromHwnd(Label1.Handle.ToInt32)
    3.  
    4.      Dim nextSlash As Integer = newDir.IndexOf("\")
    5.      Dim lastSlash As Integer = newDir.LastIndexOf("\")
    6.  
    7.      'change width for your label
    8.      If gr.MeasureString(newDir, Label1.Font).Width > 600 And nextSlash > -1 And lastSlash > nextSlash Then
    9.  
    10.         Do
    11.             Dim rLength As Integer = newDir.IndexOf("\", nextSlash + 1) - nextSlash
    12.             If rLength < 1 Then Exit Do
    13.             newDir = newDir.Replace(newDir.Substring(nextSlash + 1, rLength - 1), "...")
    14.             nextSlash = newDir.IndexOf("\", nextSlash + 1)
    15.             If gr.MeasureString(newDir, Label1.Font).Width <= 600 Then Exit Do
    16.             If nextSlash = -1 Then Exit Do
    17.         Loop
    18.      
    19.      End If
    20.  
    21.      Me.Label1.Text = newDir
    22.      Me.Label1.Refresh()
    23. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Adding "..." to long text

    Im having a look at both and i have a question.. where do u guys declare "s" and "newDir"?
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Adding "..." to long text

    Heres my problem. And if you can explain to me what exactly u did so ill remember plz and thanks

    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [2008] Adding "..." to long text

    you call it like this:

    vb Code:
    1. update_label(filename)

    it'll shorten filename + display it in label1. don't forget to read my comments in the code.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [2008] Adding "..." to long text

    i'd also recommend setting your labels autosize property to false

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] Adding "..." to long text

    Set the AutoEllipsis property to true, AutoSize to false on the label...

    That is all you have to do

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Adding "..." to long text

    But where does the "s" in "s.Substring" come from?
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  10. #10
    Lively Member
    Join Date
    Jul 2008
    Posts
    107

    Re: [2008] Adding "..." to long text

    Keymaker eh?

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [2008] Adding "..." to long text

    how about this.

    Code:
            Label2.AutoSize = False
            Label2.AutoEllipsis = True
            Label2.Text = "THIS IS WAY TO LONG FOR THE LABEL. KISS"
    i see that matt, once again, is a faster typer than i.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Adding "..." to long text

    kleinma your a funny one, thanks.. i didnt know it was that simple
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] [2008] Adding "..." to long text

    but i gave an example
    Last edited by dbasnett; Oct 22nd, 2008 at 03:50 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: [2008] Adding "..." to long text

    Quote Originally Posted by youngbucks
    Im having a look at both and i have a question.. where do u guys declare "s" and "newDir"?
    s was not declared by mistake it should have been filename. it has been corrected....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width