User Tools

Site Tools


k12:k12.4:k12.4.3:start

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 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 12.4.3.4, 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.

Download

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