' dynform.bas -- Dynamic Form Demo
'
' Demonstrates building a complete form entirely in code
' with no .frm file needed:
'   CreateForm      - create a form programmatically
'   CreateControl   - add controls with optional parent
'   SetEvent        - bind events to arbitrary handlers
'   RemoveControl   - remove a control at runtime
'   Optional params - language extension for optional arguments
'
' Add commdlg.bas to your project, then click Run.

DIM frm AS LONG
DIM lbl AS LONG
DIM btnBar AS LONG

' Create a form entirely from code
SET frm = CreateForm("DynDemo", 350, 200)
frm.Caption = "Dynamic Form Demo"

' Add a label at the top
SET lbl = CreateControl(frm, "Label", "StatusLabel")
StatusLabel.Caption = "Built entirely in code!"

' Add a horizontal button bar container
SET btnBar = CreateControl(frm, "HBox", "ButtonBar")

' Add buttons inside the HBox, using the optional parent parameter
DIM btnHello AS LONG
SET btnHello = CreateControl(frm, "CommandButton", "BtnHello", btnBar)
BtnHello.Caption = "Hello"

DIM btnFile AS LONG
SET btnFile = CreateControl(frm, "CommandButton", "BtnFile", btnBar)
BtnFile.Caption = "Open File..."

DIM btnRemove AS LONG
SET btnRemove = CreateControl(frm, "CommandButton", "BtnRemove", btnBar)
BtnRemove.Caption = "Remove Me"

' Wire events to custom handler names using SetEvent
SetEvent btnHello, "Click", "OnHelloClick"
SetEvent btnFile, "Click", "OnFileClick"
SetEvent btnRemove, "Click", "OnRemoveClick"

frm.Show

PRINT "Dynamic form created. Try the buttons!"


SUB OnHelloClick
    DIM name AS STRING
    name = basInputBox2("Greeting", "What is your name?", "World")
    IF name <> "" THEN
        StatusLabel.Caption = "Hello, " + name + "!"
        PRINT "Greeted: " + name
    END IF
END SUB


SUB OnFileClick
    DIM path AS STRING
    path = basFileOpen("Open a File", "All Files (*.*)")
    IF path <> "" THEN
        StatusLabel.Caption = "Selected: " + path
        PRINT "File: " + path
    ELSE
        StatusLabel.Caption = "No file selected."
    END IF
END SUB


SUB OnRemoveClick
    StatusLabel.Caption = "Button removed!"
    RemoveControl frm, "BtnRemove"
    PRINT "BtnRemove removed from form."
END SUB
