McBasic™Get McBasic™ ↗
Build 881
Start here · hands-on tutorial

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.

Beginner2 Forms · 4 handlersNo network or database

Step 1Open your own practice copy#

  1. 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.
  2. Download and extract the project to a writable practice folder.
  3. Open HelloForms.mcbproj in McBasic™. Keep the entire package together.
  4. Select frmMain in 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 propertyValue
CaptionHelloForms — McBasic™
Width520
Height280
MinWidth500
MinHeight210

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 / NameLeft, TopWidth × HeightCaption / TextTabIndex
TextBox
txtName
24, 32310 × 36(empty)0
CommandButton
cmdGreet
354, 32130 × 36Greet1
Label
lblGreeting
24, 98460 × 50Enter your name and click Greet.Leave default
CommandButton
cmdDetails
24, 166180 × 36Open details2

Now select frmDetails. This window only needs an output label and a close button:

frmDetails

Select the Form itself and set:

Form propertyValue
CaptionDetails — HelloForms
Width420
Height220

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 / NameLeft, TopWidth × HeightCaption / TextTabIndex
Label
lblName
24, 32370 × 44DetailsLeave default
CommandButton
cmdClose
24, 100160 × 36Close details0

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.

OwnerEventHandler to enter
frmMain → cmdGreetClickcmdGreet_Click
frmMain → cmdDetailsClickcmdDetails_Click
frmDetailsShownForm_Shown
frmDetails → cmdCloseClickcmdClose_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.

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

    lblGreeting.Caption = "Hello, " & name
End Sub

2. 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.

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

3. 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.

Forms/frmDetails.bas · complete file
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
Complete frmMain.bas — replace the file, do not append
Forms/frmMain.bas · complete file
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 Sub

Step 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#

  1. Save the project.
  2. Choose Check and resolve any diagnostic before continuing. For an unknown name, compare the Inspector Name and the code spelling.
  3. Choose Run and perform each test below.
ActionExpected resultWhat it proves
Leave the name empty; click GreetEnter a name.The empty guard runs.
Enter three spaces; click GreetEnter a name.Validation happens after trimming.
Enter “ Ada ”; click GreetHello, AdaThe normal path uses normalized text.
Enter Ada; click Open detailsA second window shows Details for AdaThe value is assigned before Shown.
Change the main input while details is openThe existing details label stays unchangedThe field is a copied value, not a live binding.
Click Close detailsOnly the details window closesUnload Me targets its own instance.
Close the main windowThe application endsStartup-Form shutdown behavior applies.

Developer CLI users can also Check the same extracted project:

Run from the extracted archive folder
mcbasic check HelloForms.mcbproj

Step 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:

Replace one statement in cmdDetails_Click
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.

Source baseline 20f4bfed6b · Build manifest
Sources & maintenance

Original web explanation, reviewed against these repository sources. API tables and example files follow the implementation.

  • docs/manual/06-writing-mcbasic.md
  • samples/HelloForms.mcbproj/Forms/frmMain.bas
  • samples/HelloForms.mcbproj/Forms/frmDetails.bas
  • samples/HelloForms.mcbproj/Forms/frmMain.form.json
  • samples/HelloForms.mcbproj/Forms/frmDetails.form.json