12.4.3 Class Dialog - Multiple Selection

The OpenFile( [ Multi ] ) method has an optional argument 'Multi' of data type 'Boolean'. If the argument 'Multi' has the value True, the user can select multiple files. The method itself returns True if the user pressed the Cancel button or False if the user pressed the OK button. In this case, the paths of the selected files are stored in a string array in the Dialog.Paths property.

In the following project, the multiple selection (MultiSelect) is used to select several image files from a directory. The selected images are then displayed in an image viewer. A simple navigation is provided.

B1
Figure 12.4.3.1: Picture Viewer (GUI)

Pressing the button 'Select images' starts the file selection dialogue and opens the following dialogue box:

B2
Figure 12.4.3.2: File open dialogue box (multi-select)

You can then navigate through the image list - in the example this contains 5 images:

B3
Figure 12.4.3.3: Image viewer

If you close the opened dialogue box via 'Cancel', the file list remains empty, because the property Dialog.Paths then has the value zero.

B4
Figure 12.4.3.4: Dialogue box is cancelled

Only a source code excerpt is displayed and provided with supplementary comments:

[1] Public Sub btnOpenFileImage_Click()
[2]   Dim sMessage1, sMessage2 As String
[3]
[4]   SetEnabled()
[5]   Dialog.Title = "Select Picture Files (Picture List)..."
[6] ' Dialog.Filter = ["*.jpg", "JPG image file", "*.png", "PNG image file", "*", "All files"]
[7] Dialog.Filter = ["*.png;*.jpg;*.jpeg;*.gif", "Image Files", "*", "All Files"]
[8]   Dialog.ShowHidden = False
[9]   Dialog.Path = Application.Path &/ "Images"
[10]
[11] ' Select picture (True -> Multiselect aktiviert)
[12]   If Dialog.Openfile(True) Then
[13]      FMain.Text = "Dialog.OpenFile(True) with Multi-Select"
[14]      PictureBoxD.Picture = Picture["Symbols/intro.jpg"]
[15]      Return
[16]   Endif
[17]
[18]   sImagePaths = New String[] ' New image list
[19]   sImagePaths = Dialog.Paths ' Save image list → Navigation
[20]   iPictureIndex = 0
[21]
[22] ' For control:
[23] ' For Each sPathName In Dialog.Paths
[24] '   Print sPathName
[25] ' Next
[26]
[27]   PictureBoxD.Picture = Picture.Load(Dialog.Paths[0]) ' Display of the first image
[28]
[29]   sMessage1 = "The image list contains exactly " & Dialog.Paths.Count & " image".
[30] sMessage2 = "The picture list contains " & Dialog.Paths.Count & " Pictures"
[31]   FMain.Text = IIf(Dialog.Paths.Count = 1, sMessage1, sMessage2)
[32]
[33] ' Switch on image navigation if the image list contains more than one image.
[34]   If Dialog.Paths.Count > 1 Then
[35]      btnNext.Enabled = True
[36]   Else
[37]      btnNext.Enabled = False
[38]   Endif
[39]
[40]   Catch
[41]     Message.Info(Error.Text)
[42] End ' btnOpenFileImage_Click()

Comment:

The complete source code can be found in the download area in the project archive.

Download