Minimal file dialog based on FileChooser GUI component

Author: Steffen Ploetz

The purpose of this snippet is
  • to show the creation and usage of a minimalistic dialog, that utilizes the FileChooser GUI component in order

  • to have an alternative to the OpenFile static method from gb.form.dialog component - which is GTK compatible (in a GTK environment, see gb.gui component), but not comfortable.

The code can be used as a template for minimalistic dialogs that use other Chooser GUI components. Only a modal single file selection is supported and only two event handler of the FileChooser GUI component, Activate and Cancel, are used. Extending the possibilities of the dialog with additional Static Public Function and function arguments is very simple.

The Gambas code file FFileChooser.class looks like:

' Gambas class file

Static Private sLastSelectedPath As String
Static Private bResult As Boolean

Static Public Function OpenSigleFileModal() As Boolean
  Dim hForm As Form

  sLastSelectedPath = ""
  bResult = False
  hForm = New FFileChooser
  hForm.ShowModal()
  Return bResult
End

Static Public Function LastSelectedPath() As String
  Return sLastSelectedPath
End

Public Sub cFileChooser_Activate()
  sLastSelectedPath = cFileChooser.SelectedPath
  bResult = True
  Me.Close
End

Public Sub cFileChooser_Cancel()
  Me.Close
End

The Gambas form file FFileChooser.form looks like:

# Gambas Form File 3.0

{ Form Form
  MoveScaled(0,0,64,53)
  Arrangement = Arrange.Vertical
  Margin = True
  { cFileChooser FileChooser
    MoveScaled(1,1,62,51)
    Expand = True
    ShowButton = True
    ReadOnly = True
  }
}

The Form property Arrangement is set to Vertical and the cFileChooser property Expand is set to True. This enables that the FileChooser control automatically adapts to changes in the size of the Form.

The cFileChooser property ShowButton is set to True. This shows the OK button, Cancel button and the filter drop-down.

The cFileChooser property ReadOnly is set to True. This qualifies the FileChooser control as a FileOpen control.

The call of the dialog looks like:

Public Sub btnFileOpen_Click()
  Dim fileChooser As FFileChooser
  Dim sLastSelectedPath As String

  If FFileChooser.OpenSigleFileModal() = True Then
    sLastSelectedPath = FFileChooser.LastSelectedPath()
  Else
    sLastSelectedPath = ""
  Endif 
End


Page revisions


Minimal file dialog based on FileChooser GUI component: Steffen Ploetz - March 28th, 2024