Build your first Mac app
Create a greeting window, reject empty input and pass a name to a second Form. You will set every control property, connect the events and enter all four handlers.
Step 1Open your own practice copy#
- Install or open an available McBasic™ development build. The application’s Mac App Store listing is not linked yet; the ZIP is source code, not an installer.
- Download and extract the project to a writable practice folder.
- Open
HelloForms.mcbprojin McBasic™. Keep the entire package together. - Select
frmMainin the project navigator and switch to Design.
The download is the finished solution. Work through it as a guided rebuild: verify the layout, reconnect each event, and replace each named source file with the complete code shown below. Do not append a second copy of an existing handler.
To build from a new project instead, choose Desktop App, create Forms named frmMain and frmDetails, and make frmMain the startup Form. Use the layout and code in the following steps.
Step 2Build the two windows#
Start with the greeting window. The TextBox holds input, the Label displays output and the two buttons perform separate actions. Select each object before changing its Inspector values. Return or Tab commits a single-line property edit.
frmMain
Select the Form itself and set:
| Form property | Value |
|---|---|
Caption | HelloForms — McBasic™ |
Width | 520 |
Height | 280 |
MinWidth | 500 |
MinHeight | 210 |
Add the following controls directly to the Form. Set Name first, then the remaining Inspector values. Positions are in device-independent points; keep Left and Top anchoring for this first exercise.
| Control / Name | Left, Top | Width × Height | Caption / Text | TabIndex |
|---|---|---|---|---|
txtName | 24, 32 | 310 × 36 | (empty) | 0 |
cmdGreet | 354, 32 | 130 × 36 | Greet | 1 |
lblGreeting | 24, 98 | 460 × 50 | Enter your name and click Greet. | Leave default |
cmdDetails | 24, 166 | 180 × 36 | Open details | 2 |
Now select frmDetails. This window only needs an output label and a close button:
frmDetails
Select the Form itself and set:
| Form property | Value |
|---|---|
Caption | Details — HelloForms |
Width | 420 |
Height | 220 |
Add the following controls directly to the Form. Set Name first, then the remaining Inspector values. Positions are in device-independent points; keep Left and Top anchoring for this first exercise.
| Control / Name | Left, Top | Width × Height | Caption / Text | TabIndex |
|---|---|---|---|---|
lblName | 24, 32 | 370 × 44 | Details | Leave default |
cmdClose | 24, 100 | 160 × 36 | Close details | 0 |
Step 3Connect each action to its event#
Select the indicated control, open its event list and connect Click to the matching procedure. Do not substitute Change for Click. For the details Form, use its Shown lifecycle event.
| Owner | Event | Handler to enter |
|---|---|---|
| frmMain → cmdGreet | Click | cmdGreet_Click |
| frmMain → cmdDetails | Click | cmdDetails_Click |
| frmDetails | Shown | Form_Shown |
| frmDetails → cmdClose | Click | cmdClose_Click |
All four handlers are parameterless Subs. Keep Private Sub …() and End Sub. An event binding and a procedure are two halves of the same connection: neither works without the other.
Step 4Enter the code, in its owning Form#
1. Read, normalize and validate the name
Open Forms/frmMain.bas in Code view. Put Option Explicit at file scope. Then enter this complete handler. Trim$ removes outer spaces; the guard stops before the success assignment when nothing remains.
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
lblGreeting.Caption = "Hello, " & name
End Sub2. Create the details window and pass its input
Enter the second handler in the same file, outside the first. Dim declares a reference; New creates a Form instance; Set assigns it. Assign DisplayName before Show because the second Form reads it when shown.
Private Sub cmdDetails_Click()
Dim details As frmDetails
Set details = New frmDetails
details.DisplayName = txtName.Text
details.Show
End Sub3. Receive the value in the second Form
Open Forms/frmDetails.bas. Enter this complete file. DisplayName is a public field outside every procedure. Form_Shown reads it into lblName; the close button unloads only this Form instance.
Option Explicit
Public DisplayName As String
Private Sub Form_Shown()
lblName.Caption = "Details for " & DisplayName
End Sub
Private Sub cmdClose_Click()
Unload Me
End SubComplete frmMain.bas — replace the file, do not append
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
lblGreeting.Caption = "Hello, " & name
End Sub
Private Sub cmdDetails_Click()
Dim details As frmDetails
Set details = New frmDetails
details.DisplayName = txtName.Text
details.Show
End SubStep 5Check the project’s startup and ownership#
This lesson has no images, packages, migration files or external services to configure. The project starts with frmMain and uses OnStartupFormClosed: closing the startup window ends the application. Closing a details window leaves the main window usable.
Open details creates a new instance each time. The current lesson intentionally copies txtName.Text unchanged to the details window, while Greet trims it. It also allows details to open with an empty name. These are useful differences to observe before you extend the behavior.
Step 6Save, Check and test every branch#
- Save the project.
- Choose Check and resolve any diagnostic before continuing. For an unknown name, compare the Inspector Name and the code spelling.
- Choose Run and perform each test below.
| Action | Expected result | What it proves |
|---|---|---|
| Leave the name empty; click Greet | Enter a name. | The empty guard runs. |
| Enter three spaces; click Greet | Enter a name. | Validation happens after trimming. |
| Enter “ Ada ”; click Greet | Hello, Ada | The normal path uses normalized text. |
| Enter Ada; click Open details | A second window shows Details for Ada | The value is assigned before Shown. |
| Change the main input while details is open | The existing details label stays unchanged | The field is a copied value, not a live binding. |
| Click Close details | Only the details window closes | Unload Me targets its own instance. |
| Close the main window | The application ends | Startup-Form shutdown behavior applies. |
Developer CLI users can also Check the same extracted project:
mcbasic check HelloForms.mcbprojStep 7Change one rule, then repeat the checks#
Make details use the same normalized input as the greeting. Replace only the assignment in cmdDetails_Click with:
details.DisplayName = Trim$(txtName.Text)Save and Check. Enter “ Ada ” and open details: its caption should now omit the outer spaces. The greeting tests and close behavior must still pass. Commit the accepted change in Git so you can find this working point again.
Next, learn event contracts, add input validation, or explore Form lifetime and modal dialogs.
20f4bfed6b · Build manifestSources & maintenance
Original web explanation, reviewed against these repository sources. API tables and example files follow the implementation.
docs/manual/06-writing-mcbasic.mdsamples/HelloForms.mcbproj/Forms/frmMain.bassamples/HelloForms.mcbproj/Forms/frmDetails.bassamples/HelloForms.mcbproj/Forms/frmMain.form.jsonsamples/HelloForms.mcbproj/Forms/frmDetails.form.json