![]() |
Languages »
VB.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
Use the .NET framework to shorten a path string with an ellipsisBy Tim GreenfieldCompact a path or any string using built in .NET framework API |
VB 8.0, Windows, .NET 2.0VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Remember the Windows API PathCompactPath[Ex] used to take a path and shorten it by inserting an ellipsis in the appropriate place to make it fit a specific pixel width? Ever wonder how to do it native .NET? There's a very simple way to do it with the TextRenderer.MeasureText method but it's relatively undocumented.
If you're familiar with this problem you might already know about using Windows.Forms.TextRenderer.DrawText or Drawing.Graphics.DrawString methods to programmatically draw a shortened string using the framework. But I wanted a way to retrieve a shortened string in memory to later use that string inside an existing control (like a menu item).
Here's a simple function to do the trick, internally I copy the string (to keep it from affecting the source string), call Windows.Forms.TextRenderer.MeasureText with the magic bitwise parameter: TextFormatFlags.ModifyString, and return the string variable that was passed into the function.
Function CompactString(ByVal MyString As String, ByVal Width As Integer,
ByVal Font As Drawing.Font,
ByVal FormatFlags As Windows.Forms.TextFormatFlags) As String
Dim Result As String = String.Copy(MyString)
TextRenderer.MeasureText(Result, Font, New Drawing.Size(Width, 0),
FormatFlags Or TextFormatFlags.ModifyString)
Return Result
End Function
To set a label to a compacted version of the string path you would call:
Label1.Text = CompactString(MyPath, Label1.Width, Label1.Font,
TextFormatFlags.PathEllipsis)
For example, the string: c:\program files\test app\runme.exe might turn into:
c:\program files\...\runme.exe depending on the font and width.
Check out the Windows.Forms.TextFormatFlags enumerated type for other options on how to compact your string. TextFormatFlags.WordEllipsis for example will insert a "..." at the end of the string instead in between parts of a path.
You might be thinking, why not just write your own algorithm to break apart a string and insert the ellipsis in a certain spot. And in fact I have seen programmers doing this but I would recommend using the framework to handle this for a couple reasons:
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Apr 2007 Editor: Sean Ewington |
Copyright 2007 by Tim Greenfield Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |