Suppose you wanted to create a dropdown control that contained an image showing a color gradient (as opposed to the common list of items). When a user clicks somewhere in the image, you want to show the selected color by filling a small PictureBox control with the selected color (see Figure 1
|Figure 1: A form showing a SmartCombo "colorpicker" control. The color selected by the user appears in the small PictureBox next to the control.
Building this control with VB6 subclassing would be difficult, but using SmartCombo it took less than 5 minutes. First, add a component reference to the VDSCombo 1.0 Type Library. The SmartCombo control appears in your Toolbox. Place a SmartCombo somewhere on the form. Next, place a PictureBox on your form, assign a picture (or draw an image onto the control), and set the PictureBox's Visible property to False. Now you want the control to display the PictureBox as the dropdown portion. To do this, trap the click event when the user clicks the down arrow button to open the combo box. In the event handling code, check the SmartCombo.IsDropped property. If the user is opening the combo (IsDropped = False), call the SmartCombo.DropDown method and pass the hWnd property of the PictureBox. If the dropdown portion is already visible, call the CloseUp method to hide it.
The preceding code prompts SmartCombo to display the contents of the PictureBox in the dropdown portion of the control. The SmartCombo constant for the down-arrow button constant is vdsDownArrow. Any code you write to trap events in the PictureBox still works. In this instance, when the user selects a color, the Picture1_MouseDown event grabs the color, extracts the red, green, and blue values, formats them into a string, and places the result in the text portion of the SmartCombo control. Listing 1 shows the relevant code for the PictureBox shown in Figure 1.
Private Sub SmartCombo1_ButtonClick( _
ByVal ButtonID As VDSCOMBOLibCtl.vdsButtonID, _
ByVal SpinningEnded As Boolean)
Static ignorenext As Boolean
Select Case ButtonID
If Not SmartCombo1.IsDropped Then
' show the dropdown portion
' The DropDown method has other
' optional parameters discussed later
' in this review
' hide the dropdown portion
' Case (handle other button clicks here)
The Point method returns the pixel color at a given point. The function returns -1 if the specified point is outside the bounds of the object calling the methodin this case, the PictureBox. The subroutine traps for that possibility and exits if it occurs.