User Tools

Site Tools


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.

Figure Picture Viewer (GUI)

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

Figure File open dialogue box (multi-select)

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

Figure 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.

Figure 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
[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"
[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
[18]   sImagePaths = New String[] ' New image list
[19]   sImagePaths = Dialog.Paths ' Save image list → Navigation
[20]   iPictureIndex = 0
[22] ' For control:
[23] ' For Each sPathName In Dialog.Paths
[24] '   Print sPathName
[25] ' Next
[27]   PictureBoxD.Picture = Picture.Load(Dialog.Paths[0]) ' Display of the first image
[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)
[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
[40]   Catch
[41]     Message.Info(Error.Text)
[42] End ' btnOpenFileImage_Click()


  • The text in the dialogue title - line 5 - already indicates the possibility to select several image files.
  • The filter in line 6 was not set because the first partial filter initially only displays images with the extension 'jpg'. However, you can look for specific images through the two partial filters.
  • With the filter in line 7, you are immediately shown all images that match the pattern in the filter.
  • Line 8 is dispensable - unless you are dealing with very secret secret photos.
  • You should not dispense with the specification of a certain directory in line 9, with which the dialogue box starts, because otherwise the user's home directory is used as the default path or the directory last used.
  • Normally, line 12 contains the statement 'If Dialog.Openfile(True) Then Return'. If the user has cancelled the dialogue with 'Cancel' → Figure, then the procedure is exited immediately. In the example, however, the window title is changed and the start screen is set before exiting.
  • If the user has selected at least one image, then a new string array is created in line 18. To store the image list - it is needed for navigating through the image display - a copy of the dialogue image list is assigned to the string array sImagePaths.
  • In lines 22 to 25, you can iterate through the image list and display the individual file paths as a check during testing.
  • Since at least one image has been selected, the first image is immediately displayed via the instruction in line 27.
  • To escape the (formal) sentence 'The image list contains 1 image', two messages are generated in lines 29 and 30 so that correct sentences - depending on the number of images - are displayed.
  • Preventively, use the Gambas instruction CATCH to catch and display errors.

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


The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k12/k12.4/k12.4.3/start.txt · Last modified: 01.02.2022 (external edit)

Page Tools