Quantcast
Channel: How to get relative path from absolute path - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Answer by CestLaGalere for How to get relative path from absolute path

$
0
0

If you have a readonly text box, could you not not make it a label and set AutoEllipsis=true?

alternatively there are posts with code for generating the autoellipsis yourself: (this does it for a grid, you would need to pass i the width for the text box instead. It isn't quite right as it hacks off a bit more than is necessary, and I haven;t got around to finding where the calculation is incorrect.it would be easy enough to modify to remove the first part of the directory rather than the last if you desire.

Private Function AddEllipsisPath(ByVal text As String, ByVal colIndex As Integer, ByVal grid As DataGridView) As String'Get the size with the column's width     Dim colWidth As Integer = grid.Columns(colIndex).Width'Calculate the dimensions of the text with the current font    Dim textSize As SizeF = MeasureString(text, grid.Font)    Dim rawText As String = text    Dim FileNameLen As Integer = text.Length - text.LastIndexOf("\")    Dim ReplaceWith As String = "\..."    Do While textSize.Width > colWidth' Trim to make room for the ellipsis        Dim LastFolder As Integer = rawText.LastIndexOf("\", rawText.Length - FileNameLen - 1)        If LastFolder < 0 Then            Exit Do        End If        rawText = rawText.Substring(0, LastFolder) + ReplaceWith + rawText.Substring(rawText.Length - FileNameLen)        If ReplaceWith.Length > 0 Then            FileNameLen += 4            ReplaceWith = ""        End If        textSize = MeasureString(rawText, grid.Font)    Loop    Return rawTextEnd FunctionPrivate Function MeasureString(ByVal text As String, ByVal fontInfo As Font) As SizeF    Dim size As SizeF    Dim emSize As Single = fontInfo.Size    If emSize = 0 Then emSize = 12    Dim stringFont As New Font(fontInfo.Name, emSize)    Dim bmp As New Bitmap(1000, 100)    Dim g As Graphics = Graphics.FromImage(bmp)    size = g.MeasureString(text, stringFont)    g.Dispose()    Return sizeEnd Function

Viewing all articles
Browse latest Browse all 25

Trending Articles