VCLua v0.3.2

Creating VCLua application step by step

Tutorial 3

Events

 tut03.lua  tut03_events.lua

 

Unhandled events may crash your VCLua application, so never forget handle correctly referred VCLua events.

In tutorial-02 the created fileOpenAction action referred to a lua function, named onOpenAction

    {name="fileOpenAction", caption="&Open...", shortcut="Ctrl+O", onexecute="onOpenAction", imageindex=0}

To simply catch the event you should write a function named onOpenAction

function onOpenAction(Sender)
    -- do nothig
    VCL.ShowMessage("Sender was "..Sender.Name.."("..Sender.ClassName..")")   
end

This sample below shows how to handle in real the event with calling the file open dialog

function onOpenAction(Sender)
    local fod = VCL.OpenDlg(mainForm)
    local fileFilter = "Any files|*.*|Text files|*.txt|Lua scripts|*.lua"
    local openOptions = "[ofAllowMultiSelect, ofFileMustExist, ofViewDetail]"
    local filename = fod:Execute({ title="Open file(s)",initialdir="./",filter=fileFilter,options=openOptions})
    fod:Free()
    if filename==nil then return end
    local s = filename
    if type(filename)=="table" then
        s = table.concat(filename,"\n")
    end
    VCL.ShowMessage("File(s) to open:\n"..s)
end

Now you have two more unhandled events left referred by actions

{name="fileExitAction", caption="&Exit", shortcut="Alt+F4", onexecute="onExitAction", imageindex=1},
{name="helpAboutAction", caption="&About", shortcut="Alt+F1", onexecute="onAboutAction", imageindex=2},

This sample below shows how to handle in real the event with calling the file open dialog

function onAboutAction()
    -- it makes no difference who is the original Sender of the event
    print("not implemented")
end

function onExitAction()
    mainForm:Close()
end

The mainForm has one unhandled event too.

mainForm._= { position="podesktopcenter", height=400, width=600, onclosequery = "onCloseQueryEventHandler" }

This sample below shows how to handle the form closing

function onCloseQueryEventHandler(Sender)
    if VCL.MessageDlg("Are you sure?","mtConfirmation",{"mbYes","mbCancel"})=="mrYes" then
        -- Ok, do the close
        return true
    end
    return false -- Reject closing window
end