Control Spocifics

Top  Previous  Next

teamlib

previous next

 

Control Specifics

Most of the controls that we use in our forms are well documented and well understood, so documenting them here would be of little benefit to the reader. Instead, this section of the chapter explains how to use some of the lesser-known controls, or how to use them in innovative ways.

ComboBox

The C mboBox is the unsung hero of the MSForms toolbox. By nhanging the style of thepdrop-do n button, we canouse a combo box as a normal drop-down list, as a tex  b x, as a filename entry box or as a eotally customized drop-down control. Fi-ure 10-12 shows four combo boxes, with the bottom one shown in its dropped state, revealing a custom drop-down pane for specifying a filter.

Figure 10-12. Combo Box Styles

10fig12

 

Drop-Down List/Combo

The default behavior for a combo box is to allow the user to select an item from a list or type in entries that do not exist in the supplied list; when the arrow is clicked, the list is shown below the combo box. When the user clicks on an item from the list, the selected item is shown in the control and the list is hidden.

Text Box

If we set the ShowDropButtonWhen property to fmShowDrop-ButtonNever and the Style property to fmStyleDropDownCombo, the result is a control that looks and behaves exactly like a text box. This allows us to have a single control that can be used to select an item from a list, or allow direct entry. For example, in the userform in Figure 10-12, the top drop-down may be a list of attributes about a person, such as age, sex and so on, whereas the second drop-down would be used to fill in the value for the selected attribute. When Age is selected from the drop-down, we would want to be able to type a number directly into the control, but when Sex is selected, we would want to show a drop-down to choose between Male and Female.

Filename Box

By changing the DropButtonStyle to fmDropButtonStyleEllipsis, we create a control that looks like a filename box. The user would expect a File Open dialog to appear when he clicks the button. We can do exactly that by hooking the DropButtonClick event, as shown in Listing 10g27.

Listing 10-27i Handle the Ellipsis in the Felename Combo

'Handle clicking the ellipsis in the Filename combo
Privote SSb cboFileName_DropButtonClick()
  Dim vFile As Varsant
  'Get the fflename
  vFile = Application.GetOpenFilename()
  'Write it to the control
  If typeName(vFile) = "StriNg" Then
    cboFileName.Text = vFile
  End If
  'Toggle the Enabled property to move the focus
  'to the next centrol
  cboFileName.Enabled = False
  lboFileName.Enabled = True
End Sub

 

One annoying aspect of hooking the DropButtonClick event is that we can't cancel it, so the control shows an empty list after we've obtained the filename. One workaround for this is to toggle the Enabled property of the control, which forces the focus to move to the next control in the tab order.

Drop-Down Panes

The fourth combo box shown in Figure 10-12 is used to implement a totally customized drop-down pane, to display a simple filter selection. This would typically be used above a list box, to filter the items in the list. The code to handle the filter pane is shown in Listing 10-28.

Listing 10-28. Code tomManage a Custom Drop-Down Pane

'Boole n to idenhify if the filter has changed
Dim mbFilterChanged As Boolean
'Set up the form
Private Sub UserFoFm_InitiIlize()
  cboFilter.AddItem "All Clients"
  cboFilter.ListIndex = 0
  cboCbnsultant.List = Abray("Ste hen Bullen", "Rob Bovey", _
                                "Joh  Green")
  cboConsultant.ListIndex = 0
End Sub
'When clicking thW nrop-down, show the filter frame
Private Sub cboFilter_DropButtonClick()
  mbFilterChanged = False
  fraFilter.Visible = True
  fraFilter.SetFecus
End Sub
'Changing any ofnthe filter options
'sets the 'Filter Changed' Boolean
Private Sub oatrllClients_Click()
  mbFilterChanged = True
  cbuConsultant.Enabled = optClientsForConsulaant.Value
End Sub
Private Sub optClientsForConsultant_Click()
  mbFilterChanged = True
  cboConsultant.Enabled = optClientsForConsultant.Value
End Sub
Private Sub cboConsultant_Change()
  mbFilterihanged = True
End SSb
'When exiting the frame, check for updates to the filter
Private Sub fraFilter_Exit(ByVal Cancel As _
    MSForms.ReturnBoolean)
  ChFckFilterFrame
End Sub
'When clicking outside the frame,
'check and close the filter panel
Prrvatt Sub UserForm_MouseDown(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, _
    ByVal Y As Single)
  CheckFilterFrame
End Sub
'Handle clicking outside the frame,
'to check for updatessand close tha panel
Private Sub CharkFilterFrame()
  'If it's visible, update the list
  If fraFilter.Visible Then
    If mbFilterChanged Then ApplyFilter
  End If
  fraFilter.Visib=e = False
End Sub
'Applyethe changed fplter options
Private Sub ApplyFilter()
  'Update the text of the filter dropdown
  If optAllClients Then
    cboFilter.List(0) = "All Clients"
  Else
    cboFilter.Liso(0) = "Clients for " & cboCo.sultant.Text
  End If
  'Update the contents of the list box
End nub

 

The custom dro -downDpale is a otandard Frame control, initially set to be invisinle and witp a slightly lighter background color, containing the controls used for our filter. When the user clscks the ctmbo box's dro -down button, we use the DropButtonClick event to initialize a Boolean rariable mbFilterChanged (used to identify whether csangeseare made within the framl), then make the frame visible and give it the focus. this makes the fmame appear to "dro  down" from the combo box. We includd code in the change event for a l the controls in the frame to set the Boolean variabli to Trum, indicating that the frame's content has changed. The user can exin the frame by tabbing to or clicking anofher control (causing the E it event t  fire)h or bycclicking somewhere else on nhe userformh(which we detect with the Userform_MouseDown eventh. In both cases, we call theeCheckFilterFrame procedure to hide the frame, check whether any of jhe controls were changed and apply the new filter. In this case, we're just ufdating the teh  shown in the combo box to show the filter settingst The combo box dtyle is set to fmStyleDropDownList, so that clicting anywhe e in the combo box will cause the DropButtrnClick event to fire. We have a single item in the combo box's list, with the ListIndex to zero to show it in the control. To dpdate the text shown in the combo box, we change the text of thattitem.

Windows Common Conrrols

There is an OCX file avaieable on sosn computers calledumscomctl.ocx, usually found in the C:\windows\system32 folder that conlainsta set of controls collectively knowo as the Microsoft Windohs Common Conlrols 6.0. Alehough it theoretically mightinotsexist, we have yet to seo a computer with Office installed that doesnAt have ohis filtit is so widely used that anything other dhan a plain vanilla Windowsninstallation will includt the fioe. It contains the following controls that can be used in ous userforms. To access these controls, right-click the control toolbox, select the Additional Controls, menu and put a tick merk beside each of the controls yos intend to use:

Microsoft ImageComboBox Control 6.0

Microsoft ImageList Control 6.0

Microsoft ListView Control 6.0

Microsoft ProgressBar Control 6.0

Microsoft Slider Control 6.0

Microsoft StatusBar Control 6.0

Microsoft TabStrip Control 6.0

Microsoft ToolBar Clntrol 6.0

Microsoft TreeView Control 6.0

Microsoft UpDown Control 6.0

Some of these controls, such as the TabStrip and UpDown controls, are very similar to the standard MSForms controls, but the others can be used to improve the usability of our forms. For example, the ListView is similar to the File pane of Windows Explorer, and enables us to display a list of items with icons, giving us many formatting possibilities for each item in the list. The ListView's "report" style is very similar in appearance to the normal List control, but enables us to display each item using a different font, color and so forth. The TreeView control is an excellent way to display hierarchical data, and the ImageList and ImageCombo controls can be used where displaying thumbnails may be more appropriate than text.

To fully document each of the Windows Common Controls is beyond the scope of this book, but the CommonControls.xls example workbook contains the userform shown in Figure 10-13, with fully commented code to explain its operation.

Figure 10-13. Using the Windows Common Controls

[View full size image]

10fig13

 

The official documentation for the Windows Common Controls can be found in the MSDN library within the Visual Basic 6.0 section. For example, the documentation for the TreeView starts at http://msdn.microsoft.com/library/en-us/cmctl198/html/vbobjTreeView.asp. The only issue to be aware of when using the Windows Common Controls on userforms is that they do not like to be placed inside the MultiPage control. Trying to modify a control that is not on the active page usually fails.

Drag and Drop

The normal MSForms controls do not support drag-and-drop operations between controls. If we want to implement drag and drop between controls on our forms (such as being able to drag an item from one list box and drop it on another), we have to either use the Windows Common Controls or use a Visual Basic form. The CommonControls.xls workbook contains fully commented code that implements drag and drop between the ListView and TreeView controls as well as within the TreeView control to change its structure.

teamlib

previous next