McBasic™Get McBasic™ ↗
Build 881
Hands-on tutorial · Language

Share behavior between Forms

Put reusable code in a standard module instead of duplicating event logic.

2 Form / control designs3 complete BASIC filesGuided sample project

Form code should handle interaction; a module can own a calculation or text transformation used by several Forms. Keep inputs explicit and avoid depending on whichever window happens to be active. Public exposes an API; Private keeps an implementation detail inside the module.

Step 1Prepare a safe practice project#

Extract the archive into a new writable folder, separate from your own application. Open SharedModules.mcproj 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
SharedModules.mcproj
{
  "schemaVersion": 1,
  "projectId": "5c2bcc0d-8ed6-43e9-974d-c70d2b9333a1",
  "name": "SharedModules",
  "kind": "DesktopApp",
  "language": {
    "dialect": "McBasic1",
    "requireExplicit": true,
    "compare": "Binary",
    "defaultArrayBase": 0
  },
  "application": {
    "bundleIdentifier": "com.mcbasic.samples.sharedmodules",
    "version": "0.1.0",
    "buildNumber": "100",
    "startupFormId": "form-main",
    "shutdownMode": "OnStartupFormClosed"
  },
  "forms": [
    {
      "id": "form-main",
      "typeName": "frmMain",
      "layout": "Forms/frmMain.form.json",
      "code": "Forms/frmMain.bas"
    },
    {
      "id": "form-details",
      "typeName": "frmDetails",
      "layout": "Forms/frmDetails.form.json",
      "code": "Forms/frmDetails.bas"
    }
  ],
  "modules": [
    {
      "name": "Utilities",
      "path": "Modules/Utilities.bas"
    }
  ],
  "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"Shared Modules — McBasic"
Width520
Height280
MinWidth500
MinHeight210
TypeNameParent
TextBoxtxtNameForm
CommandButtoncmdGreetForm
LabellblGreetingForm
CommandButtoncmdDetailsForm
txtName · exact Inspector values
PropertyValue
Left24
Top32
Width310
Height36
Text""
TabIndex0
Anchor edgesLeft, Top
cmdGreet · exact Inspector values
PropertyValue
Left354
Top32
Width130
Height36
Caption"Greet"
TabIndex1
Anchor edgesLeft, Top
lblGreeting · exact Inspector values
PropertyValue
Left24
Top98
Width460
Height50
Caption"Enter your name and click Greet."
Anchor edgesLeft, Top
cmdDetails · exact Inspector values
PropertyValue
Left24
Top166
Width180
Height36
Caption"Open details"
TabIndex2
Anchor edgesLeft, Top

frmDetails

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"Shared Module Details"
Width420
Height220
TypeNameParent
LabellblNameForm
CommandButtoncmdCloseForm
lblName · exact Inspector values
PropertyValue
Left24
Top32
Width370
Height44
Caption"Details"
Anchor edgesLeft, Top
cmdClose · exact Inspector values
PropertyValue
Left24
Top100
Width160
Height36
Caption"Close details"
TabIndex0
Anchor edgesLeft, Top

Step 3Connect the events explicitly#

frmMain

ObjectEventProcedureParameters
cmdGreetClickcmdGreet_ClickClick()
cmdDetailsClickcmdDetails_ClickClick()

frmDetails

ObjectEventProcedureParameters
cmdCloseClickcmdClose_ClickClick()
FormShownForm_Shown()

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 cmdGreet_Click()

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

Forms/frmMain.bas · cmdGreet_Click
Private Sub cmdGreet_Click()
    Dim name As String
    name = Trim$(txtName.Text)

    If Len(name) = 0 Then
        lblGreeting.Caption = "Enter a name."
        Exit Sub
    End If

    Dim count As Integer
    count = 40
    Call Utilities.BumpTwice(count)
    lblGreeting.Caption = Utilities.Greeting(name) & " " & count
End Sub
Private Sub cmdDetails_Click()

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

Forms/frmMain.bas · cmdDetails_Click
Private Sub cmdDetails_Click()
    Dim details As frmDetails
    Set details = New frmDetails
    details.DisplayName = txtName.Text
    details.Show
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 cmdGreet_Click()
    Dim name As String
    name = Trim$(txtName.Text)

    If Len(name) = 0 Then
        lblGreeting.Caption = "Enter a name."
        Exit Sub
    End If

    Dim count As Integer
    count = 40
    Call Utilities.BumpTwice(count)
    lblGreeting.Caption = Utilities.Greeting(name) & " " & count
End Sub

Private Sub cmdDetails_Click()
    Dim details As frmDetails
    Set details = New frmDetails
    details.DisplayName = txtName.Text
    details.Show
End Sub

Forms/frmDetails.bas

Enter these declarations at file scope, before the procedures.

Forms/frmDetails.bas · declarations
Option Explicit

Public DisplayName As String
Private Sub Form_Shown()

Initialize state before enabling dependent actions. Keep the shared declarations and startup dependencies with this procedure.

Forms/frmDetails.bas · Form_Shown
Private Sub Form_Shown()
    lblName.Caption = "Details for " & DisplayName
End Sub
Private Sub cmdClose_Click()

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

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

Replace the file once; do not append duplicate procedures.

Forms/frmDetails.bas
Option Explicit

Public DisplayName As String

Private Sub Form_Shown()
    lblName.Caption = "Details for " & DisplayName
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

Modules/Utilities.bas

Enter these declarations at file scope, before the procedures.

Modules/Utilities.bas · declarations
Option Explicit
Public Function Greeting(ByVal name As String) As String

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

Modules/Utilities.bas · Greeting
Public Function Greeting(ByVal name As String) As String
    Greeting = "Shared hello, " & name
End Function
Public Sub Bump(ByRef value As Long)

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

Modules/Utilities.bas · Bump
Public Sub Bump(ByRef value As Long)
    value = value + 1
End Sub
Public Sub BumpTwice(ByRef value As Variant)

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

Modules/Utilities.bas · BumpTwice
Public Sub BumpTwice(ByRef value As Variant)
    Call BumpAliases(value, value)
End Sub
Private Sub BumpAliases(ParamArray values() As Variant)

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

Modules/Utilities.bas · BumpAliases
Private Sub BumpAliases(ParamArray values() As Variant)
    Call BumpEach(values)
End Sub
Private Sub BumpEach(ByRef values() As Variant)

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

Modules/Utilities.bas · BumpEach
Private Sub BumpEach(ByRef values() As Variant)
    Dim index As Long
    For index = LBound(values) To UBound(values)
        values(index) = values(index) + 1
    Next index
End Sub
Copy the complete Modules/Utilities.bas file

Replace the file once; do not append duplicate procedures.

Modules/Utilities.bas
Option Explicit

Public Function Greeting(ByVal name As String) As String
    Greeting = "Shared hello, " & name
End Function

Public Sub Bump(ByRef value As Long)
    value = value + 1
End Sub

' Two arguments refer to the same caller; both increments must be visible.
Public Sub BumpTwice(ByRef value As Variant)
    Call BumpAliases(value, value)
End Sub

Private Sub BumpAliases(ParamArray values() As Variant)
    Call BumpEach(values)
End Sub

Private Sub BumpEach(ByRef values() As Variant)
    Dim index As Long
    For index = LBound(values) To UBound(values)
        values(index) = values(index) + 1
    Next index
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 SharedModules.mcproj
mcbasic run SharedModules.mcproj

Acceptance checklist

  • Run the greeting on the main Form.
  • Open details and compare the shared behavior.
  • Change the module implementation once and verify both callers.

Step 7Make one change and repeat the tests#

Add a pure formatting function and call it from both Forms.

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