McBasic™Get McBasic™ ↗
Build 881
Hands-on tutorial · Data & services

Use native pickers safely

Open file, save, folder, color, font and print-related dialogs.

1 Form / control designs1 complete BASIC filesGuided sample project

A cancelled dialog is a normal outcome. Read the result before performing a file operation, and never turn an empty selection into a default path silently. A picker grants the user a choice; it does not mean the later read or write cannot fail.

Step 1Prepare a safe practice project#

Extract the archive into a new writable folder, separate from your own application. Open CommonDialog.mcbproj in McBasic™. Keep every declared source, layout, resource and migration file together.

To reconstruct it yourself, create a DesktopApp project and add the files declared below. In the complete project, follow these steps as a guided rebuild: replace each named source file once and Check after all its procedures are present.

Exact project configuration
project.mcproj
{
  "schemaVersion": 1,
  "projectId": "f6b21cf4-c273-4af0-a809-52f1fdce390b",
  "name": "CommonDialogSample",
  "kind": "DesktopApp",
  "ai": {
    "storeChatsInProject": false
  },
  "language": {
    "dialect": "McBasic1",
    "requireExplicit": true,
    "compare": "Binary",
    "defaultArrayBase": 0
  },
  "application": {
    "bundleIdentifier": "com.mcbasic.samples.commondialog",
    "version": "0.1.0",
    "buildNumber": "1",
    "startupFormId": "form-main",
    "shutdownMode": "OnStartupFormClosed"
  },
  "forms": [
    {
      "id": "form-main",
      "typeName": "frmMain",
      "layout": "Forms/frmMain.form.json",
      "code": "Forms/frmMain.bas"
    }
  ],
  "modules": [],
  "classes": [],
  "tests": [],
  "references": [],
  "resources": [],
  "targets": [
    "osx-arm64"
  ],
  "capabilities": []
}

Step 2Build the Form and set its properties#

frmMain

Select the Form first and set its properties. Add containers before their children. These are initial Designer values, not statements to paste into a procedure.

Form propertyValue
Caption"CommonDialog — McBasic"
Width760
Height430
MinWidth680
MinHeight380
TypeNameParent
CommandButtoncmdOpenForm
CommandButtoncmdSaveForm
CommandButtoncmdFolderForm
CommandButtoncmdColorForm
CommandButtoncmdFontForm
CommandButtoncmdPrinterForm
CommandButtoncmdPageSetupForm
CommandButtoncmdCloseForm
LabellblStatusForm
CommonDialogdlgCommonComponent tray
cmdOpen · exact Inspector values
PropertyValue
Left28
Top34
Width150
Height36
Caption"Open file"
TabIndex0
Anchor edgesLeft, Top
cmdSave · exact Inspector values
PropertyValue
Left196
Top34
Width150
Height36
Caption"Save file"
TabIndex1
Anchor edgesLeft, Top
cmdFolder · exact Inspector values
PropertyValue
Left364
Top34
Width150
Height36
Caption"Choose folder"
TabIndex2
Anchor edgesLeft, Top
cmdColor · exact Inspector values
PropertyValue
Left532
Top34
Width150
Height36
Caption"Choose color"
TabIndex3
Anchor edgesRight, Top
cmdFont · exact Inspector values
PropertyValue
Left28
Top92
Width150
Height36
Caption"Choose font"
TabIndex4
Anchor edgesLeft, Top
cmdPrinter · exact Inspector values
PropertyValue
Left196
Top92
Width150
Height36
Caption"Printer settings"
TabIndex5
Anchor edgesLeft, Top
cmdPageSetup · exact Inspector values
PropertyValue
Left364
Top92
Width150
Height36
Caption"Page setup"
TabIndex6
Anchor edgesLeft, Top
cmdClose · exact Inspector values
PropertyValue
Left532
Top92
Width150
Height36
Caption"Close"
TabIndex7
Anchor edgesRight, Top
lblStatus · exact Inspector values
PropertyValue
Left28
Top168
Width654
Height120
Caption"Choose a CommonDialog action. Cancel is reported separately from an error."
ForeColor"#263238"
Anchor edgesLeft, Top, Right, Bottom
dlgCommon · exact Inspector values
PropertyValue
Title"McBasic CommonDialog"
Filter"Text files|*.txt;*.md"
FileName"notes.txt"
Color"#1565C0"
Font"System|13|Regular|Normal"
PrinterName""
PaperName""
PageOrientation"Portrait"
PageWidth612
PageHeight792

Step 3Connect the events explicitly#

frmMain

ObjectEventProcedureParameters
cmdOpenClickcmdOpen_ClickClick()
cmdSaveClickcmdSave_ClickClick()
cmdFolderClickcmdFolder_ClickClick()
cmdColorClickcmdColor_ClickClick()
cmdFontClickcmdFont_ClickClick()
cmdPrinterClickcmdPrinter_ClickClick()
cmdPageSetupClickcmdPageSetup_ClickClick()
cmdCloseClickcmdClose_ClickClick()

Step 4Enter the complete behavior#

Open the named source file. Enter declarations first, then every procedure. Expand a procedure for its full body, or copy the complete file. Helpers are required: do not stop after entering the first Click handler.

Forms/frmMain.bas

Enter these declarations at file scope, before the procedures.

Forms/frmMain.bas · declarations
Option Explicit
Private Sub ShowResult(ByVal Value As String)

Enter this helper in the named source file, outside every other procedure. Its callers are included in the same project.

Forms/frmMain.bas · ShowResult
Private Sub ShowResult(ByVal Value As String)
    If dlgCommon.Cancelled Then
        lblStatus.Caption = dlgCommon.LastAction + " cancelled"
    Else
        lblStatus.Caption = dlgCommon.LastAction + ": " + Value
    End If
End Sub
Private Sub cmdOpen_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdOpen_Click
Private Sub cmdOpen_Click()
    dlgCommon.ShowOpen
    ShowResult dlgCommon.FileName
End Sub
Private Sub cmdSave_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdSave_Click
Private Sub cmdSave_Click()
    dlgCommon.ShowSave
    ShowResult dlgCommon.FileName
End Sub
Private Sub cmdFolder_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdFolder_Click
Private Sub cmdFolder_Click()
    dlgCommon.ShowFolder
    ShowResult dlgCommon.FileName
End Sub
Private Sub cmdColor_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdColor_Click
Private Sub cmdColor_Click()
    dlgCommon.ShowColor
    ShowResult dlgCommon.Color
End Sub
Private Sub cmdFont_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdFont_Click
Private Sub cmdFont_Click()
    dlgCommon.ShowFont
    ShowResult dlgCommon.Font
End Sub
Private Sub cmdPrinter_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdPrinter_Click
Private Sub cmdPrinter_Click()
    dlgCommon.ShowPrinter
    ShowResult dlgCommon.PrinterName + " / " + dlgCommon.PaperName
End Sub
Private Sub cmdPageSetup_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdPageSetup_Click
Private Sub cmdPageSetup_Click()
    dlgCommon.ShowPageSetup
    ShowResult dlgCommon.PageOrientation + " " + CStr(dlgCommon.PageWidth) + " x " + CStr(dlgCommon.PageHeight)
End Sub
Private Sub cmdClose_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdClose_Click
Private Sub cmdClose_Click()
    Unload Me
End Sub
Copy the complete Forms/frmMain.bas file

Replace the file once; do not append duplicate procedures.

Forms/frmMain.bas
Option Explicit

Private Sub ShowResult(ByVal Value As String)
    If dlgCommon.Cancelled Then
        lblStatus.Caption = dlgCommon.LastAction + " cancelled"
    Else
        lblStatus.Caption = dlgCommon.LastAction + ": " + Value
    End If
End Sub

Private Sub cmdOpen_Click()
    dlgCommon.ShowOpen
    ShowResult dlgCommon.FileName
End Sub

Private Sub cmdSave_Click()
    dlgCommon.ShowSave
    ShowResult dlgCommon.FileName
End Sub

Private Sub cmdFolder_Click()
    dlgCommon.ShowFolder
    ShowResult dlgCommon.FileName
End Sub

Private Sub cmdColor_Click()
    dlgCommon.ShowColor
    ShowResult dlgCommon.Color
End Sub

Private Sub cmdFont_Click()
    dlgCommon.ShowFont
    ShowResult dlgCommon.Font
End Sub

Private Sub cmdPrinter_Click()
    dlgCommon.ShowPrinter
    ShowResult dlgCommon.PrinterName + " / " + dlgCommon.PaperName
End Sub

Private Sub cmdPageSetup_Click()
    dlgCommon.ShowPageSetup
    ShowResult dlgCommon.PageOrientation + " " + CStr(dlgCommon.PageWidth) + " x " + CStr(dlgCommon.PageHeight)
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

Step 5Keep resources and storage reproducible#

No external resource or migration files are declared. Any inline migration, seed data or local JSON is present in the code above. Runtime application data and private preferences are not included in the archive.

Step 6Check, run and test the result#

Save all files and run Check. Resolve diagnostics before Run. Developer CLI users can work from the extracted folder:

Developer CLI
mcbasic check CommonDialog.mcbproj
mcbasic run CommonDialog.mcbproj

Acceptance checklist

  • Open each dialog and cancel it.
  • Choose a test file and a writable save destination.
  • Exercise color and font selection.
  • Open printer and page setup dialogs without sending a print job.

Step 7Make one change and repeat the tests#

After a save selection, write a small text file only when the returned path is nonempty.

Save a working Git checkpoint first. Change one behavior, Check again and repeat the acceptance checklist. Compare with the downloaded solution if a regression appears.

Source baseline 20f4bfed6b · Build manifest