Add a Check Box to the Left Hand Side of a Drop-Down Combo Box

In Microsoft's Outlook Express, there is a find window which allows you to set a date range to search in. You turn this date range on and off by clicking a check box which is embedded in the left hand side of the combo box. The text of the combo box is moved to the left to accommodate the check box.

This tip shows you how to emulate this effect by setting the left margin of a drop-down combo box.

Start a new project, then add a Combo Box and a Check Box to your form. Resize the Check Box so only the check box portion itself is showing (not the text). Then add the following code 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 FindWindowEx Lib "user32" _
   Alias "FindWindowExA" _
   (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, _
   ByVal lpszClass As String, _
   ByVal lpszWindow As String) As Long
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 Sub AddCheckToCombo( _
         ByRef chkThis As CheckBox, _
         ByRef cboThis As ComboBox _
         )
   Dim lhWnd As Long
   Dim lMargin As Long
   lhWnd = FindWindowEx(cboThis.hwnd, 0, "EDIT", vbNullString)
   If (lhWnd <> 0) Then
      lMargin = chkThis.Width \ Screen.TwipsPerPixelX + 2
      SendMessageLong lhWnd, EM_SETMARGINS, EC_LEFTMARGIN, lMargin
      chkThis.BackColor = cboThis.BackColor
      chkThis.Move _
         cboThis.Left + 3 * Screen.TwipsPerPixelX, _
         cboThis.Top + 2 * Screen.TwipsPerPixelY, _
         chkThis.Width, _
         cboThis.Height - 4 * Screen.TwipsPerPixelY
      chkThis.ZOrder
   End If

End Sub

Private Sub Form_Load()
   AddCheckToCombo Check1, Combo1
   Dim i As Long
   For i = 1 To 20
      Combo1.AddItem "Test" & i
   Next i
End Sub

Start the project. The text in the combo box will be shifted across to accommodate the check box, and you can click the combo box on and off.