-
Oct 21st, 2014, 08:10 AM
#1
[RESOLVED] Get ellipse string??
I know how to draw ellipsed text with text with the textrender and gdi plus ... but how can I get an ellipse string???
Eg:
GetElipsedText("Hello there this is a test", 250, font)
will return something like: "Hello there this..."
I assume there is a built in way to do this, but can't find anything
Thanks,
Kris
-
Oct 21st, 2014, 09:10 AM
#2
Re: Get ellipse string??
I don't believe there is a built in way (although a Label control has AutoEllipsis property).
Even in most browser, the ellipsis function is built from a small xml class.
If you can't find one that someone else has done for .Net, you'll have to write one.
If you can draw ellipsed text already, wouldn't the logic be similar for a function to return an ellipsed string.
p.s. A quick search found this link...
But, that is VB6. Should be easier in VB.net.
p.p.s Check out this question and response concerning VB.Net... MichaelB's "solution" has the same parameters you gave, just in a different order.
Looks like a more direct .Net method here. which is probably along the lines that you are doing now to draw the string.
The key there is to use the ModifyString flag of the TextRenderer.MeasureText to modify the string passed to the MeasureText method.
I was a little surprised it didn't put the ellipsis at a white space break for either PathEllipsis or EndEllipsis, but I guess that is the standard for missing text.
Be aware that PathEllipsis only works on a validly formatted path. If you want to put an ellipsis in the middle of an arbitrary string, you have to write more code to handle that, one example of that is here.
EndEllipsis or WordEllipsis works fine for putting Ellipsis at the end of an arbitrary string.
Last edited by passel; Oct 21st, 2014 at 09:55 AM.
-
Oct 21st, 2014, 09:22 AM
#3
Re: Get ellipse string??
Originally Posted by passel
I don't believe there is a built in way (although a Label control has AutoEllipsis property).
Even in most browser, the ellipsis function is built from a small xml class.
If you can't find one that someone else has done for .Net, you'll have to write one.
If you can draw ellipsed text already, wouldn't the logic be similar for a function to return an ellipsed string.
p.s. A quick search found this link...
But, that is VB6. Should be easier in VB.net.
p.p.s Check out this question and response concerning VB.Net...
Yea ... I know how to do it ... would be easy enough with measurestring ... just thought there would be some inbuilt method ... that's all
Kris
-
Oct 21st, 2014, 11:18 AM
#4
Re: Get ellipse string??
The text renderer has some flags that can be used to add ellipsis:
Code:
Public Class Form1
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim styles As Windows.Forms.ControlStyles = ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.ResizeRedraw Or _
ControlStyles.UserPaint
Me.SetStyle(styles, True)
End Sub
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim f As New Drawing.Font("Tahoma", 128.0)
TextRenderer.DrawText(e.Graphics, "Hello there this is a test", f, Me.ClientRectangle, Color.Black, TextFormatFlags.EndEllipsis)
End Sub
End Class
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Oct 21st, 2014, 03:05 PM
#5
Re: Get ellipse string??
Originally Posted by i00
Yea ... I know how to do it ... would be easy enough with measurestring ... just thought there would be some inbuilt method ... that's all
Kris
And all I was saying is there isn't a built in as far as I know, but using the MeasureText function to modify the string and return it is essentially a one liner so about as close to a "built-in" as having a provided function, as you do have to pass the string you want returned as a parameter to be modified, as opposed to a function returning a string.
That was the example in the last link I posted.
So using it directly could be done like in this button click
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim s As String = "Hello there this is a test"
TextRenderer.MeasureText(s, Font, New Size(100, 0), TextFormatFlags.EndEllipsis Or TextFormatFlags.ModifyString)
Debug.Print(s)
End Sub
But, you could wrap those few lines into a function to match your desired example.
Code:
Private Function GetElipsedText(s As String, pixelWidth As Integer, fnt As Font) As String
Dim r As String = s
TextRenderer.MeasureText(r, fnt, New Size(pixelWidth, 0), TextFormatFlags.EndEllipsis Or TextFormatFlags.ModifyString)
Return r 'return the modified string, if modification was necessary
End Function
-
Oct 21st, 2014, 07:23 PM
#6
Re: Get ellipse string??
Originally Posted by SJWhiteley
The text renderer has some flags that can be used to add ellipsis:
...
If you read my original post
Originally Posted by i00
I know how to draw ellipsed text with text with the textrender and gdi plus ... but how can I get an ellipse string???
...you would have known that this is not what I want ... I have sorted it now though.
Kris
-
Oct 22nd, 2014, 06:29 AM
#7
Re: [RESOLVED] Get ellipse string??
Okay, snarky, I read it as did NOT know how to get ellipsed string...
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
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
|