Use native pickers safely
Open file, save, folder, color, font and print-related dialogs.
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
{
"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 property | Value |
|---|---|
Caption | "CommonDialog — McBasic" |
Width | 760 |
Height | 430 |
MinWidth | 680 |
MinHeight | 380 |
| Type | Name | Parent |
|---|---|---|
| CommandButton | cmdOpen | Form |
| CommandButton | cmdSave | Form |
| CommandButton | cmdFolder | Form |
| CommandButton | cmdColor | Form |
| CommandButton | cmdFont | Form |
| CommandButton | cmdPrinter | Form |
| CommandButton | cmdPageSetup | Form |
| CommandButton | cmdClose | Form |
| Label | lblStatus | Form |
| CommonDialog | dlgCommon | Component tray |
cmdOpen · exact Inspector values
| Property | Value |
|---|---|
Left | 28 |
Top | 34 |
Width | 150 |
Height | 36 |
Caption | "Open file" |
TabIndex | 0 |
| Anchor edges | Left, Top |
cmdSave · exact Inspector values
| Property | Value |
|---|---|
Left | 196 |
Top | 34 |
Width | 150 |
Height | 36 |
Caption | "Save file" |
TabIndex | 1 |
| Anchor edges | Left, Top |
cmdFolder · exact Inspector values
| Property | Value |
|---|---|
Left | 364 |
Top | 34 |
Width | 150 |
Height | 36 |
Caption | "Choose folder" |
TabIndex | 2 |
| Anchor edges | Left, Top |
cmdColor · exact Inspector values
| Property | Value |
|---|---|
Left | 532 |
Top | 34 |
Width | 150 |
Height | 36 |
Caption | "Choose color" |
TabIndex | 3 |
| Anchor edges | Right, Top |
cmdFont · exact Inspector values
| Property | Value |
|---|---|
Left | 28 |
Top | 92 |
Width | 150 |
Height | 36 |
Caption | "Choose font" |
TabIndex | 4 |
| Anchor edges | Left, Top |
cmdPrinter · exact Inspector values
| Property | Value |
|---|---|
Left | 196 |
Top | 92 |
Width | 150 |
Height | 36 |
Caption | "Printer settings" |
TabIndex | 5 |
| Anchor edges | Left, Top |
cmdPageSetup · exact Inspector values
| Property | Value |
|---|---|
Left | 364 |
Top | 92 |
Width | 150 |
Height | 36 |
Caption | "Page setup" |
TabIndex | 6 |
| Anchor edges | Left, Top |
cmdClose · exact Inspector values
| Property | Value |
|---|---|
Left | 532 |
Top | 92 |
Width | 150 |
Height | 36 |
Caption | "Close" |
TabIndex | 7 |
| Anchor edges | Right, Top |
lblStatus · exact Inspector values
| Property | Value |
|---|---|
Left | 28 |
Top | 168 |
Width | 654 |
Height | 120 |
Caption | "Choose a CommonDialog action. Cancel is reported separately from an error." |
ForeColor | "#263238" |
| Anchor edges | Left, Top, Right, Bottom |
dlgCommon · exact Inspector values
| Property | Value |
|---|---|
Title | "McBasic CommonDialog" |
Filter | "Text files|*.txt;*.md" |
FileName | "notes.txt" |
Color | "#1565C0" |
Font | "System|13|Regular|Normal" |
PrinterName | "" |
PaperName | "" |
PageOrientation | "Portrait" |
PageWidth | 612 |
PageHeight | 792 |
Step 3Connect the events explicitly#
frmMain
| Object | Event | Procedure | Parameters |
|---|---|---|---|
cmdOpen | Click | cmdOpen_Click | Click() |
cmdSave | Click | cmdSave_Click | Click() |
cmdFolder | Click | cmdFolder_Click | Click() |
cmdColor | Click | cmdColor_Click | Click() |
cmdFont | Click | cmdFont_Click | Click() |
cmdPrinter | Click | cmdPrinter_Click | Click() |
cmdPageSetup | Click | cmdPageSetup_Click | Click() |
cmdClose | Click | cmdClose_Click | Click() |
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.
Option ExplicitPrivate 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.
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 SubPrivate Sub cmdOpen_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdOpen_Click()
dlgCommon.ShowOpen
ShowResult dlgCommon.FileName
End SubPrivate Sub cmdSave_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdSave_Click()
dlgCommon.ShowSave
ShowResult dlgCommon.FileName
End SubPrivate Sub cmdFolder_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdFolder_Click()
dlgCommon.ShowFolder
ShowResult dlgCommon.FileName
End SubPrivate Sub cmdColor_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdColor_Click()
dlgCommon.ShowColor
ShowResult dlgCommon.Color
End SubPrivate Sub cmdFont_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdFont_Click()
dlgCommon.ShowFont
ShowResult dlgCommon.Font
End SubPrivate Sub cmdPrinter_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdPrinter_Click()
dlgCommon.ShowPrinter
ShowResult dlgCommon.PrinterName + " / " + dlgCommon.PaperName
End SubPrivate Sub cmdPageSetup_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdPageSetup_Click()
dlgCommon.ShowPageSetup
ShowResult dlgCommon.PageOrientation + " " + CStr(dlgCommon.PageWidth) + " x " + CStr(dlgCommon.PageHeight)
End SubPrivate Sub cmdClose_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdClose_Click()
Unload Me
End SubCopy the complete Forms/frmMain.bas file
Replace the file once; do not append duplicate procedures.
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 SubStep 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:
mcbasic check CommonDialog.mcbproj
mcbasic run CommonDialog.mcbprojAcceptance 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.
20f4bfed6b · Build manifest