User Tools

Site Tools


k12:k12.4:k12.4.1:start

12.4.1 Dialogue class (gb.form.dialog)

The component defines extended versions of the standard dialogues, based on the standard dialogues defined in the gb.qt4 component for calling dialogue boxes. This chapter introduces properties and methods of the Dialog class (gb.form.dialog) and supplements them with examples.

12.4.1.1 Properties

The Dialog (gb.form.dialog) class has these properties:

PropertyDataTypeDescription
AutoExtBooleanReturns the value or set with True that the file extension is set as default file extension in the filter in the file save dialogue.
FixedOnlyBooleanReturns the value or sets with True that only non-proportional fonts should be displayed in the font selection dialogue.
ShowHiddenBooleanReturns the value or sets with True that also hidden files should be shown in the dialogue box.
ColorIntegerDetermines the colour selected in the colour selection dialogue box or sets the colour in the colour selection dialogue box as the default colour.
DateDateReturns the date selected in the dialogue or sets the date in the calendar as the default date.
FilterString[]Returns the filter used in the dialogue (file extension) or sets the filter. Filter is a string array where each entry consists of a filter and a filter description. A filter is a list of file extensions with placeholders separated by a semicolon. A filter description can be any string. A filter is automatically appended to the filter description when displayed.
FontFontDetermines the font selected in the font selection dialogue or sets the font in the font selection dialogue as the default font.
PathStringReturns the selected file path in the file selection dialog as string or sets the file path in the file dialog as default path.
PathsString[]Returns the selected file paths in the file open dialog as a string array.
TitleStringReturns the (window) title selected in the dialogue or sets the title for the dialogue.

Table 12.4.1.1 : Properties of the class Dialog (gb.form.dialog)

12.4.1.2 Methods

Class Dialog (gb.form.dialog) has the following methods. Note optional arguments.

MethodDescription
OpenFile( [ Multi As Boolean ] ) As BooleanInvokes the file open dialog to retrieve the file name of the file to be opened. If the optional argument 'Multi' has the value False (default), the user can select exactly one file. The return value is the path to the selected file - stored in the path property Dialog.Path. If the optional argument 'Multi' has the value True, the user can select multiple files. The return value is a string array containing the paths to all selected files - stored in the Dialog.Paths property. The method itself returns True if the user has pressed the Cancel button or False if the user has pressed the OK button.
SaveFile( ) As BooleanCalls the (standard) File Save sub-dialog to query the file name of the file to be saved. The method itself returns True if the user pressed the Cancel button or False if the user pressed the OK button.
SelectDirectory( ) As BooleanCalls the file standard dialogue to read out an existing directory name. The method itself returns True if the user pressed the Cancel button or False if the user pressed the OK button.
SelectFont( ) As BooleanCalls the standard font selection dialogue. The method itself returns True if the user pressed the Cancel button or False if the user pressed the OK button.
SelectColor() As BooleanCalls the standard colour selection dialogue. The method itself returns True if the user pressed the Cancel button or False if the user pressed the OK button.
SelectDate( ) As BooleanCalls the control 'DateChooser' to select a date. The method itself returns True if the user pressed the Cancel button or False if the user pressed the OK button.

Table 12.4.1.2.1 : Methods of the class Dialog (gb.form.dialog)

12.4.1.3 Examples

In order to be able to follow all the examples, you will find a project archive in the download area. For this reason, the complete source text is not included here. Only source code excerpts are presented in which important passages are highlighted in colour and the results achieved are displayed.

Example 1 - Dialog.SelectDirectory()

In the first example, a (existing) directory is selected:

Public Sub btnSelectDirectory_Click()
  Dialog.Title = "Select an image directory ..."
  Dialog.Path = Application.Path
  If Dialog.SelectDirectory() Then Return
 
  sImageDirectoryPath = Dialog.Path
  btnOpenFileImage.Enabled = True
End ' btnSetFont_Click()

B1
Figure 12.4.1.3.1: Directory selection dialogue box

Example 2 - Dialog.Openfile(..) - Image files.

Exactly one specific (image) file is to be selected from the directory selected in the first example - the optional argument has the value False (default). In addition to the file filter, it is specified that hidden files are not displayed:

Public Sub btnOpenFileImage_Click()
  Dialog.Title = "Select an image file ..."
' Dialog.Filter = ["*.jpg", "JPG image file", "*.png", "PNG image file", "*", "All files"]
  ' Dialog.Filter = ["*.png;*.jpg;*.jpeg;*.gif", "Image Files", "*", "All Files"]
  Dialog.ShowHidden = False
  If Not sImageDirectoryPath Then Dialog.Path = Application.Path &/ "Images"
  If Dialog.Openfile(False) Then Return ' Select exactly 1 file (False -> Multiselect switched off)
 
  sImagePath = Dialog.Path ' Secure Path
  PictureBoxD.Picture = Picture.Load(Dialog.Path)
  FMain.Text = "The image file is processed:  " & File.Name(Dialog.Path)
  btnOpenFileText.Enabled = True
  Catch
    Message.Info(Error.Text)
End ' btnOpenFileImage_Click()

B2
Figure 12.4.1.3.2: File Open dialogue box - 1

The first filter is not used, but the activated one as a compact filter list. With the first filter, the individual filters are displayed one below the other in the combo box.

Example 3 - Dialog.Openfile(..) - Text Files

Exactly one text file with the extension txt is selected. In this project, the individual lines have a specific format.

Public Sub btnOpenFileText_Click()
  Dim i As Integer
  Dim aTextArray, aImagetext As String[]
 
  Dialog.Title = "Select an image description file ..."
  Dialog.Filter = ["*.txt", "Text Files", "*", "All Files"]
  Dialog.Path = Application.Path &/ "Images"
  If Dialog.OpenFile(False) Then Return ' Select exactly 1 file (False -> Multiselect switched off)
 
  aTextArray = Split(File.Load(Dialog.Path), gb.NewLine) ' Each text line is an element in the array
  aImageText = New String[2] ' Create another array with 2 elements
  For i = 0 To 1
  ' Only the first two lines are split at the separator ':' and the 2nd part is stored in each case
    aImagetext[i] = Split(aTextArray[i], ":")[1]
  Next
 
  lblImagetext1.Text = Trim(aImagetext[0])
  lblImageText2.Text = Trim(aImageText[1])
  btnSelectFont.Enabled = True
  Catch
    Message.Info(Error.Text)
End ' btnOpenFileText_Click()

This is what the first lines in the text file look like:

Image title: Flora of the Alps
Image title2: Eyebright (Euphrasia minima) - Allgäu - 2012
Camera model: COOLPIX L22
Name: image1.jpg
Width: 510 Pixel
Height: 294 Pixel
Type: JPEG image
Bytes: 42.6 kB
Aperture value: f/5.5
...

B3
Figure 12.4.1.3.3: File open dialogue box - 2

Example 4 - Dialog.SelectFont().

In addition to the title of the dialogue box, the font of a particular label control is also set as the default font. In addition, it is agreed that all existing fonts are available for selection and not only the non-proportional fonts are displayed. You can also do without this agreement, as the default is 'Dialog.FixedOnly = False'.

B4
Figure 12.4.1.3.4: Font dialogue box

Public Sub btnSelectFont_Click()
  Dialog.Title = "Select a font ..."
  Dialog.Font = lblImageText1.Font
  Dialog.FixedOnly = False
  If Dialog.SelectFont() Then Return
 
  lblImageText1.Font = Dialog.Font
  lblImageText1.Font.Size = 0.7 * Dialog.Font.Size
  lblImageText2.Font = Dialog.Font
  lblImageText2.Font.Size = 0.7 * 0.8 * Dialog.Font.Size
  LblImageText2.Font.Bold = False
  hFont = Dialog.Font ' Save current font
  lblImageText2.Tooltip = lblImageText2.Text
  btnSelectColor.Enabled = True
End ' btnSelectFont_Click()

Example 5 - Dialog.SelectColor().

To add colour to the game, you can call the Colour dialogue. In addition to the dialogue title, the colour white is declared as the default colour.

Public Sub btnSelectColor_Click()
  Dialog.Title = "Select a colour ..."
  Dialog.Color = Color.White
  If Dialog.SelectColor() Then Return
  lblImageText1.Foreground = Dialog.Color
  lblImageText2.Foreground = Color.White
  hColor = Dialog.Color ' Save current colour
  btnPreview.Enabled = True
End ' btnSelectColor_Click()

B5
Figure 12.4.1.3.5: Colour dialogue box

Example 6 - Dialog.SaveFile().

In addition to setting the dialogue box title, default path and file filter, the Dialog.AutoExt property, which is only present in the SaveFile dialogue, is also set to True. This has the effect that you can enter the file name of the file to be saved without an extension; this is added automatically → Figure 12.4.1.3.6 bottom left.

B6
Figure 12.4.1.3.6: File Save dialogue box.

Public Sub btnSaveFileImage_Click()
  Dialog.Title = "Save the changed image ..."
  Dialog.Filter = ["*.png", "PNG image file", "*", "All files"]
  Dialog.Path = Application.Path &/ "Results/"
  Dialog.AutoExt = True
  If Dialog.SaveFile() Then Return
' File.Save(Dialog.Path, PictureBoxD.Picture) ' Not allowed - only strings are saved!
  PictureBoxD.Picture.Save(Dialog.Path, 100) ' hImage.Save(Dialog.Path, 100) ' → Alternative
  GetReset()
  Catch
    Message.Info(Error.Text)
End ' btnSaveFileImage_Click()

Example 7 - Dialog.Date().

This dialogue for selecting a date is special in that it calls the DateChooser that exists in the gb.form component. You can again specify the dialogue title and a date; in the example set to the current date:

Public Sub btnSelectDate_Click()
  Dialog.Title = "Select a date ..."
  Dialog.Date = Now()
  If Dialog.SelectDate() Then Return
  dDate = Dialog.Date ' Save selected date
  ' Print Format(dDate, "dddd - dd. mmmm yyyy")
End ' btnSelectDate_Click()

B7
Figure 12.4.1.3.7: Dialogue → DateChooser

The source codes used above were taken from a project that is available in the download section for your own experiments:

B8
Figure 12.4.1.3.8: Programme interface

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.1/start.txt · Last modified: 01.02.2022 (external edit)

Page Tools