Results 1 to 7 of 7

Thread: [RESOLVED] Get ellipse string??

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Resolved [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

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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.

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Get ellipse string??

    Quote Originally Posted by passel View Post
    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

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Get ellipse string??

    Quote Originally Posted by i00 View Post
    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

  6. #6

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Get ellipse string??

    Quote Originally Posted by SJWhiteley View Post
    The text renderer has some flags that can be used to add ellipsis:
    ...
    If you read my original post

    Quote Originally Posted by i00 View Post
    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

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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
  •  



Click Here to Expand Forum to Full Width