McBasic™Get McBasic™ ↗
Build 881
Hands-on tutorial · Lists & navigation

Choose from a visible list

Keep a list selection and its output synchronized.

1 Form / control designs1 complete BASIC filesGuided sample project

A ListBox keeps alternatives visible. Its Change handler should read the selected state, not infer it from the mouse position. Check initial and empty selection states so code does not index into an item that does not exist.

Step 1Prepare a safe practice project#

Extract the archive into a new writable folder, separate from your own application. Open ListBox.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
ListBox.mcproj
{
  "schemaVersion": 1,
  "projectId": "d3a4c981-3896-478a-9675-9959057ca966",
  "name": "ListBoxSample",
  "kind": "DesktopApp",
  "language": {
    "dialect": "McBasic1",
    "requireExplicit": true,
    "compare": "Binary",
    "defaultArrayBase": 0
  },
  "application": {
    "bundleIdentifier": "com.mcbasic.samples.listbox",
    "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 propertyValue
Caption"ListBox — McBasic"
Width520
Height260
MinWidth420
MinHeight220
TypeNameParent
ListBoxlstChoiceForm
LabellblStatusForm
lstChoice · exact Inspector values
PropertyValue
Left36
Top48
Width260
Height120
Items"First choice\nSecond choice\nThird choice"
SelectionMode"Multiple"
SortedTrue
Padding"8,6,8,6"
BorderStyle"Solid"
BorderColor"#5B6F92"
BorderWidth2
CornerRadius6
HorizontalScrollBar"Auto"
VerticalScrollBar"Auto"
SelectedIndex0
TabIndex0
Anchor edgesLeft, Top
lblStatus · exact Inspector values
PropertyValue
Left36
Top188
Width420
Height40
Caption"Choose an item from the list."
Anchor edgesLeft, Top

Step 3Connect the events explicitly#

frmMain

ObjectEventProcedureParameters
lstChoiceChange
Alias of SelectionChanged
lstChoice_ChangeChange()
FormLoadForm_Load()

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

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

Forms/frmMain.bas · Form_Load
Private Sub Form_Load()
    Dim selectedItems As Collection
    Dim selectedIndices As Collection
    lstChoice.Items.Add "Fourth choice"
    lstChoice.Items.RemoveAt 1
    lstChoice.SelectedItem = "Fourth choice"
    Set selectedItems = lstChoice.SelectedItems
    Set selectedIndices = lstChoice.SelectedIndices
    lblStatus.Caption = CStr(lstChoice.Items.Count) & " sorted=" & CStr(lstChoice.Sorted) & " — " & CStr(lstChoice.SelectedItem) & " — " & CStr(selectedItems.Count) & " selected at " & CStr(selectedIndices.Item(1)) & " — padding " & lstChoice.Padding & " — border " & lstChoice.BorderStyle & " " & lstChoice.BorderColor & " " & CStr(lstChoice.BorderWidth) & "/" & CStr(lstChoice.CornerRadius) & " — scroll " & lstChoice.HorizontalScrollBar & "/" & lstChoice.VerticalScrollBar
End Sub
Private Sub lstChoice_Change()

Read the current value or selection and update its dependent interface. The binding above determines which object calls this handler.

Forms/frmMain.bas · lstChoice_Change
Private Sub lstChoice_Change()
    Dim itemList As Collection
    Dim selectedItems As Collection
    Dim selectedIndices As Collection
    Dim selected As Variant
    Set itemList = lstChoice.Items
    Set selectedItems = lstChoice.SelectedItems
    Set selectedIndices = lstChoice.SelectedIndices
    selected = lstChoice.SelectedItem
    If lstChoice.SelectedIndex >= 0 Then
        lblStatus.Caption = CStr(lstChoice.SelectedCount) & " selected in " & lstChoice.SelectionMode & " mode at " & CStr(selectedIndices.Item(1))
    Else
        lblStatus.Caption = "No item is selected."
    End If
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 Form_Load()
    Dim selectedItems As Collection
    Dim selectedIndices As Collection
    lstChoice.Items.Add "Fourth choice"
    lstChoice.Items.RemoveAt 1
    lstChoice.SelectedItem = "Fourth choice"
    Set selectedItems = lstChoice.SelectedItems
    Set selectedIndices = lstChoice.SelectedIndices
    lblStatus.Caption = CStr(lstChoice.Items.Count) & " sorted=" & CStr(lstChoice.Sorted) & " — " & CStr(lstChoice.SelectedItem) & " — " & CStr(selectedItems.Count) & " selected at " & CStr(selectedIndices.Item(1)) & " — padding " & lstChoice.Padding & " — border " & lstChoice.BorderStyle & " " & lstChoice.BorderColor & " " & CStr(lstChoice.BorderWidth) & "/" & CStr(lstChoice.CornerRadius) & " — scroll " & lstChoice.HorizontalScrollBar & "/" & lstChoice.VerticalScrollBar
End Sub

Private Sub lstChoice_Change()
    Dim itemList As Collection
    Dim selectedItems As Collection
    Dim selectedIndices As Collection
    Dim selected As Variant
    Set itemList = lstChoice.Items
    Set selectedItems = lstChoice.SelectedItems
    Set selectedIndices = lstChoice.SelectedIndices
    selected = lstChoice.SelectedItem
    If lstChoice.SelectedIndex >= 0 Then
        lblStatus.Caption = CStr(lstChoice.SelectedCount) & " selected in " & lstChoice.SelectionMode & " mode at " & CStr(selectedIndices.Item(1))
    Else
        lblStatus.Caption = "No item is selected."
    End If
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 ListBox.mcproj
mcbasic run ListBox.mcproj

Acceptance checklist

  • Select different items with mouse and keyboard.
  • Check the initial selected index.
  • Change the item list and repeat selection.

Step 7Make one change and repeat the tests#

Add a command that operates only when an item is selected.

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