Visual Basic Programming Code Examples Visual Basic > Files Directories Drives Code Examples Compact a file path to fit a control Compact a file path to fit a control The following code autofits text in a control by replacing a portion of the text with an ellipsis (...), if the text is wider than a control. This is typically used for showing variable length paths in a label control. The code uses the PathCompactPath function in SHLWAPI. The PathCompactPath function requires three arguments. The first argument contains a device context handle. The second argument holds the address of the pathname you want to use. The third argument contains the width in pixels of the control in which you want the text: Option Explicit Private Declare Function PathCompactPath Lib "shlwapi.dll" Alias "PathCompactPathA" (ByVal hDC As Long, ByVal pszPath As String, ByVal dx As Long) As Long 'Purpose : Compacts a path to fit a control and stores the original value in the tag. 'Inputs : oForm The form the control is resides ' oControl The control to display the path on ' sFilePath The file path to display on the control 'Outputs : The compacted path. This will include "..." for paths which have been compacted 'Notes : 'Revisions : 'Assumptions : Function ControlFitPath(oForm As Form, oControl As Control, sFilePath As String) As String Dim lhDC As Long, lCtlWidth As Long, lRet As Long Const clBorder As Long = 5 On Error Resume Next oControl.Tag = sFilePath 'Store the original path in the control's tag If oForm.ScaleMode = vbPixels Then lCtlWidth = oControl.Width - clBorder Else lCtlWidth = (oControl.Width / Screen.TwipsPerPixelX) - clBorder End If lhDC = oForm.hDC ControlFitPath = sFilePath Call PathCompactPath(lhDC, ControlFitPath, lCtlWidth) lRet = InStr(1, ControlFitPath, vbNullChar) If lRet Then 'Trim off characters after null ControlFitPath = Left$(ControlFitPath, lRet - 1) End If End Function 'Example, uses a label on a form. Private Sub Form_Load() Label1.Caption = ControlFitPath(Me, Label1, "C:\MyPath\MyPath\MyPath\MyPath\MyPath\ENDOFPATHNAME") End Sub