Multi Select

The application in Figure 11-7 has been modified to allow the user to select multiple filenames by holding down Shift or Ctrl while clicking filenames. The filenames are then loaded into the combo box, called ComboBox1, at the top of the screen, from which the files can be chosen for viewing.

Figure 11-7

The code has been modified as follows:

Private Sub cmdGetFile_Click() Dim fd As FileDialog Dim ffs As FileDialogFilters

Dim vltem

On Error GoTo Problem 'Set up File | Open dialog

Set fd = Application.FileDialog(msoFileDialogOpen) With fd

'Clear default filters and create picture filter Set ffs = .Filters

With ffs .Clear

.Add "Pictures", "*.jpg" End With

'Allow multiple file selection .AllowMultiSelect = True

'Show the dialog. Exit if Cancel is pressed If .Show = False Then Exit Sub

'Load selected files into combo box ComboBox1.Clear

For Each vItem In .SelectedItems

ComboBox1.AddItem vItem Next vItem

'Display first file ComboBox1.ListIndex = 0

End With Exit Sub Problem:

MsgBox "That was not a valid picture" End Sub

Private Sub ComboBox1_Change()

Imagel.Picture = LoadPicture(ComboBoxl.Text) End Sub

You set the AllowMultiSelect property to True. The combo box list is cleared of any previous items, and you use a For Each...Next loop to add the items in the FileDialogSelectedItems collection to the combo box list. When you set the combo box Listindex property to 0, it triggers the Change event and the event procedure loads the first picture into the image control.

0 0

Post a comment