Fit Filenames Into a Specified Width

You will need: Command1 and Label1.

' In general declarations
Private Declare Function PathCompactPath Lib "shlwapi" Alias "PathCompactPathA" (ByVal hDC As Long, ByVal lpszPath As String, ByVal dx As Long) As Long

' On a  Form
Private Sub Command1_Click()
   Dim lhDC As Long, lCtlWidth As Long
   Dim FileSpec As String
   
   FileSpec = "C:\MyFolder\VisualBasic\MyReallyWayTooLongFolderName\ButWhoCares\IhaveTheAPI.doc"
   
   Me.ScaleMode = vbPixels
   
   lCtlWidth = Label1.Width - Me.DrawWidth
   lhDC = Me.hDC
   
   PathCompactPath lhDC, FileSpec, lCtlWidth
   
   Label1.Caption = FileSpec
End Sub

Click the button and see that the FileSpec variable is shortened to fit inside the label1's width.

You might also like...

Comments

Kym Manson Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski