Share behavior between Forms
Put reusable code in a standard module instead of duplicating event logic.
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
{
"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 property | Value |
|---|---|
Caption | "Shared Modules — McBasic" |
Width | 520 |
Height | 280 |
MinWidth | 500 |
MinHeight | 210 |
| Type | Name | Parent |
|---|---|---|
| TextBox | txtName | Form |
| CommandButton | cmdGreet | Form |
| Label | lblGreeting | Form |
| CommandButton | cmdDetails | Form |
txtName · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 32 |
Width | 310 |
Height | 36 |
Text | "" |
TabIndex | 0 |
| Anchor edges | Left, Top |
cmdGreet · exact Inspector values
| Property | Value |
|---|---|
Left | 354 |
Top | 32 |
Width | 130 |
Height | 36 |
Caption | "Greet" |
TabIndex | 1 |
| Anchor edges | Left, Top |
lblGreeting · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 98 |
Width | 460 |
Height | 50 |
Caption | "Enter your name and click Greet." |
| Anchor edges | Left, Top |
cmdDetails · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 166 |
Width | 180 |
Height | 36 |
Caption | "Open details" |
TabIndex | 2 |
| Anchor edges | Left, 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 property | Value |
|---|---|
Caption | "Shared Module Details" |
Width | 420 |
Height | 220 |
| Type | Name | Parent |
|---|---|---|
| Label | lblName | Form |
| CommandButton | cmdClose | Form |
lblName · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 32 |
Width | 370 |
Height | 44 |
Caption | "Details" |
| Anchor edges | Left, Top |
cmdClose · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 100 |
Width | 160 |
Height | 36 |
Caption | "Close details" |
TabIndex | 0 |
| Anchor edges | Left, Top |
Step 3Connect the events explicitly#
frmMain
| Object | Event | Procedure | Parameters |
|---|---|---|---|
cmdGreet | Click | cmdGreet_Click | Click() |
cmdDetails | Click | cmdDetails_Click | Click() |
frmDetails
| Object | Event | Procedure | Parameters |
|---|---|---|---|
cmdClose | Click | cmdClose_Click | Click() |
| Form | Shown | Form_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.
Option ExplicitPrivate Sub cmdGreet_Click()
Enter the complete action, including its input checks and calls to helper procedures.
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 SubPrivate Sub cmdDetails_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdDetails_Click()
Dim details As frmDetails
Set details = New frmDetails
details.DisplayName = txtName.Text
details.Show
End SubCopy the complete Forms/frmMain.bas file
Replace the file once; do not append duplicate procedures.
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 SubForms/frmDetails.bas
Enter these declarations at file scope, before the procedures.
Option Explicit
Public DisplayName As StringPrivate Sub Form_Shown()
Initialize state before enabling dependent actions. Keep the shared declarations and startup dependencies with this procedure.
Private Sub Form_Shown()
lblName.Caption = "Details for " & DisplayName
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/frmDetails.bas file
Replace the file once; do not append duplicate procedures.
Option Explicit
Public DisplayName As String
Private Sub Form_Shown()
lblName.Caption = "Details for " & DisplayName
End Sub
Private Sub cmdClose_Click()
Unload Me
End SubModules/Utilities.bas
Enter these declarations at file scope, before the procedures.
Option ExplicitPublic 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.
Public Function Greeting(ByVal name As String) As String
Greeting = "Shared hello, " & name
End FunctionPublic 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.
Public Sub Bump(ByRef value As Long)
value = value + 1
End SubPublic 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.
Public Sub BumpTwice(ByRef value As Variant)
Call BumpAliases(value, value)
End SubPrivate 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.
Private Sub BumpAliases(ParamArray values() As Variant)
Call BumpEach(values)
End SubPrivate 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.
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 SubCopy the complete Modules/Utilities.bas file
Replace the file once; do not append duplicate procedures.
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 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 SharedModules.mcproj
mcbasic run SharedModules.mcprojAcceptance 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.
20f4bfed6b · Build manifest