Set Left and Right Margin of Text Boxes and Combo Boxes

This tip demonstrates how to modify the left and right margins of Drop-Down Combo Boxes and Text box controls.

Start a new project then add a text box and a combo box control to the form:

Option Explicit

Private Const EC_LEFTMARGIN = &H1
Private Const EC_RIGHTMARGIN = &H2
Private Const EC_USEFONTINFO = &HFFFF&
Private Const EM_SETMARGINS = &HD3&
Private Const EM_GETMARGINS = &HD4&

Private Declare Function SendMessageLong Lib "user32" _
   Alias "SendMessageA" _
   (ByVal hwnd As Long, ByVal wMsg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindowEx Lib "user32" _
   Alias "FindWindowExA" _
   (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, _
   ByVal lpszClass As String, _
   ByVal lpszWindow As String) As Long

Private Property Get EdithWnd(ByVal ctl As Control) As Long
   If TypeName(ctl) = "ComboBox" Then
      EdithWnd = FindWindowEx(ctl.hwnd, 0, "EDIT", vbNullString)
   ElseIf TypeName(ctl) = "TextBox" Then
      EdithWnd = ctl.hwnd
   End If
End Property
Public Property Let RightMargin(ByVal ctl As Control, ByVal lMargin As Long)
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      SendMessageLong lhWnd, EM_SETMARGINS, EC_RIGHTMARGIN, lMargin * &H10000
   End If
End Property
Public Property Get RightMargin(ByVal ctl As Control) As Long
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      RightMargin = SendMessageLong(lhWnd, EM_GETMARGINS, 0, 0) \ &H10000
   End If
End Property
Public Property Let LeftMargin(ByVal ctl As Control, ByVal lMargin As Long)
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      SendMessageLong lhWnd, EM_SETMARGINS, EC_LEFTMARGIN, lMargin
   End If
End Property
Public Property Get LeftMargin(ByVal ctl As Control) As Long
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      LeftMargin = (SendMessageLong(lhWnd, EM_GETMARGINS, 0, 0) And &HFFFF&)
   End If
End Property


Private Sub Form_Load()
   LeftMargin(Text1) = EC_USEFONTINFO
   LeftMargin(Combo1) = EC_USEFONTINFO
End Sub

Start the project. The left hand margin of the text and combo boxes will be set to the same size. You can use the EC_USEFONTINFO (&HFFFF&) constant to set the margin to the default position, or you can set the exact amount in pixels.