Results 1 to 7 of 7

Thread: [2008][RESOLVED] Remove text longer then ... px

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Resolved [2008][RESOLVED] Remove text longer then ... px

    Hello,

    I'm developing a word-teaching program. When choosen, it shows wich chapters you're busy with. But if they get too long for the form, I want to change the last 3 characters shown on the form to be "...".
    Then it would be "Chapter 1, 2, 3, ..." instead of "Chapter 1, 2, 3, 4, 5, 6" from wich the half isn't visible anymore.

    I've tried to calculate on the textlength, but since not every character equals in width, this doesn't work properly. My calculations take place in a function wich multiple forms use.
    A piece of my original function Code:
    1. If sender = "frmFout" Then
    2.      If tekst.Length > 31 Then tekst = tekst.Remove(31, tekst.Length - 31) & "..."
    3. ElseIf sender = "frmOverhoor" Then
    4.      If tekst.Length > 18 Then tekst = tekst.Remove(18, tekst.Length - 18) & "..."
    5. ElseIf sender = "Print" Then
    6.      If tekst.Length > 57 Then tekst = tekst.Remove(57, tekst.Length - 57) & "..."
    7. End If

    How can I do this on a different way and make it work properly?

    With kind regards,

    Thomas
    Last edited by Mindstorms; May 17th, 2008 at 06:34 AM. Reason: Resolved

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

    Re: [2008] Remove text longer then ... px

    One thing you can do is to set the font to a mono-spaced one, like Lucida or Courier. Then all the characters are the same size.
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2008] Remove text longer then ... px

    That's a good one. It doesn't look really nice, but when there's no (relativily simple) other solution I'll use thisone.

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

    Re: [2008] Remove text longer then ... px

    you could decide on a value that always works and just add a lot of "...."
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2008] Remove text longer then ... px

    Should also be a possibility

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2008] Remove text longer then ... px

    The Graphics.MeasureString might be able to assist you
    Code:
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            Dim g As Graphics = Me.CreateGraphics
            Dim sf As SizeF = g.MeasureString(TextBox1.Text, TextBox1.Font, TextBox1.MaximumSize)
            If sf.Width > 20 Then
                'do whatever
            End If
        End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Resolved Re: [2008] Remove text longer then ... px

    I messed a little bit with your sample and got it to work the way I want.

    Sample Code:
    1. Private Function CalculateWrap(ByVal text As String, ByVal px As Integer, ByVal font As Font) As String
    2.     '//
    3.     Dim g As Graphics = Me.CreateGraphics
    4.     Dim pt As SizeF = g.MeasureString(" ...", Font)
    5.     If Not g.MeasureString(text, font).Width < px Then '//Doesn't fit at once
    6.         Do
    7.             text = text.Remove(text.Length - 1, 1)
    8.         Loop Until g.MeasureString(text, font).Width < (px - pt.Width)
    9.         text = text & " ..."
    10.     End If
    11.     '//
    12.     Return text
    13. End Function

    Thanks!
    Last edited by Mindstorms; May 20th, 2008 at 10:11 AM.

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