The new vbAccelerator Site - more VB and .NET Code and Controls

Add a Check Box to the left hand side of a Drop-Down Combo Box

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Combo Box,Windows,Windows Controls

Updated:

21/02/99

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


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.


&nbsp

Related Tips and Articles:

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 21/02/99