基于 FileChooser GUI 组件的最小文件对话框

Author: PZ0151

这段代码的目的是
  • 显示简约对话框的创建和使用,该对话框利用 FileCooser GUI 组件合乎逻辑

  • 有一个 gb.form.dialog 组件中 OpenFile 静态方法的替代方法 - 它与 GTK 兼容(在 GTK 环境中,请参阅 gb.gui 组件),但不太方便。

该代码可用作使用其他选择器 GUI 组件的简约对话框的模板。 Only a modal single file selection is supported and only two event handler of the FileCooser 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