|
-
May 16th, 2008, 02:16 PM
#1
Thread Starter
Addicted Member
[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:
If sender = "frmFout" Then
If tekst.Length > 31 Then tekst = tekst.Remove(31, tekst.Length - 31) & "..."
ElseIf sender = "frmOverhoor" Then
If tekst.Length > 18 Then tekst = tekst.Remove(18, tekst.Length - 18) & "..."
ElseIf sender = "Print" Then
If tekst.Length > 57 Then tekst = tekst.Remove(57, tekst.Length - 57) & "..."
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
-
May 16th, 2008, 02:18 PM
#2
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.
-
May 16th, 2008, 02:34 PM
#3
Thread Starter
Addicted Member
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.
-
May 16th, 2008, 02:38 PM
#4
Re: [2008] Remove text longer then ... px
you could decide on a value that always works and just add a lot of "...."
-
May 16th, 2008, 02:40 PM
#5
Thread Starter
Addicted Member
Re: [2008] Remove text longer then ... px
Should also be a possibility
-
May 16th, 2008, 02:46 PM
#6
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
-
May 17th, 2008, 05:52 AM
#7
Thread Starter
Addicted Member
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:
Private Function CalculateWrap(ByVal text As String, ByVal px As Integer, ByVal font As Font) As String
'//
Dim g As Graphics = Me.CreateGraphics
Dim pt As SizeF = g.MeasureString(" ...", Font)
If Not g.MeasureString(text, font).Width < px Then '//Doesn't fit at once
Do
text = text.Remove(text.Length - 1, 1)
Loop Until g.MeasureString(text, font).Width < (px - pt.Width)
text = text & " ..."
End If
'//
Return text
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|