OnDgsPropertyChange

From Multi Theft Auto: Wiki
Revision as of 02:44, 10 July 2025 by Mohab (talk | contribs) (Created page with "__NOTOC__ {{Client event}} This event is triggered when a monitored property of a dgs element changes via dgsSetProperty. The property must be added to the listener using dgsAddPropertyListener for this event to fire. {{Note|This event only fires for properties that have been explicitly added as listeners using dgsAddPropertyListener}} {{Note|The event only triggers when using dgsSetProperty, not element-specific functions like dgsSetText}} {{Tip|Thi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This event is triggered when a monitored property of a dgs element changes via dgsSetProperty. The property must be added to the listener using dgsAddPropertyListener for this event to fire.

[[{{{image}}}|link=|]] Note: This event only fires for properties that have been explicitly added as listeners using dgsAddPropertyListener
[[{{{image}}}|link=|]] Note: The event only triggers when using dgsSetProperty, not element-specific functions like dgsSetText
[[{{{image}}}|link=|]] Tip: This event does NOT fire for user input changes (like typing in edit boxes). For monitoring user text input, use onDgsTextChange instead
[[{{{image}}}|link=|]] Note: The event will not fire if the new value is the same as the old value
[[{{{image}}}|link=|]] Tip: This event is designed for programmatic property changes, not user interactions

Parameters

string propertyName, mixed newValue, mixed oldValue
  • propertyName: the name of the property that changed
  • newValue: the new value that was set
  • oldValue: the previous value before the change

Source

The source is the dgs element whose property was changed.

Example

This example shows how to monitor window position changes:

DGS = exports.dgs
local window = DGS:dgsCreateWindow(200, 200, 400, 300, "Property Monitor", false)

-- Add listener for position changes
DGS:dgsAddPropertyListener(window, "absPos")

-- Handle property changes
function handlePropertyChange(propertyName, newValue, oldValue)
    if propertyName == "absPos" then
        outputChatBox("Window moved from " .. oldValue[1] .. "," .. oldValue[2] ..
                     " to " .. newValue[1] .. "," .. newValue[2])
    end
end
addEventHandler("onDgsPropertyChange", window, handlePropertyChange)

-- Test the listener
setTimer(function()
    DGS:dgsSetProperty(window, "absPos", {300, 250})
end, 2000, 1)

This example demonstrates onDgsPropertyChange with programmatic text updates:

DGS = exports.dgs
local window = DGS:dgsCreateWindow(300, 200, 320, 320, "Property Change Demo", false)
local nameEdit = DGS:dgsCreateEdit(20, 50, 280, 30, "", false, window)
local emailEdit = DGS:dgsCreateEdit(20, 100, 280, 30, "", false, window)
local submitBtn = DGS:dgsCreateButton(20, 150, 280, 40, "Submit (Disabled)", false, window)
local statusLabel = DGS:dgsCreateLabel(20, 200, 280, 40, "Click buttons to set text", false, window)
local setNameBtn = DGS:dgsCreateButton(20, 250, 135, 30, "Set Name", false, window)
local setEmailBtn = DGS:dgsCreateButton(165, 250, 135, 30, "Set Email", false, window)

DGS:dgsCreateLabel(20, 30, 100, 20, "Name:", false, window)
DGS:dgsCreateLabel(20, 80, 100, 20, "Email:", false, window)

-- Monitor text property changes
DGS:dgsAddPropertyListener(nameEdit, "text")
DGS:dgsAddPropertyListener(emailEdit, "text")

function validateForm()
    local name = DGS:dgsGetProperty(nameEdit, "text")
    local email = DGS:dgsGetProperty(emailEdit, "text")

    local isValid = (name and name ~= "" and email and email ~= "" and string.find(email, "@"))

    DGS:dgsSetProperty(submitBtn, "enabled", isValid)
    if isValid then
        DGS:dgsSetProperty(submitBtn, "text", "Submit (Ready!)")
        DGS:dgsSetProperty(statusLabel, "text", "Form is valid!")
        DGS:dgsSetProperty(statusLabel, "textColor", tocolor(0, 255, 0, 255))
    else
        DGS:dgsSetProperty(submitBtn, "text", "Submit (Disabled)")
        DGS:dgsSetProperty(statusLabel, "text", "Fill both fields")
        DGS:dgsSetProperty(statusLabel, "textColor", tocolor(255, 0, 0, 255))
    end
end

function handlePropertyChange(propertyName, newValue, oldValue)
    outputChatBox("Property '" .. propertyName .. "' changed to: " .. tostring(newValue))
    validateForm()
end

addEventHandler("onDgsPropertyChange", nameEdit, handlePropertyChange)
addEventHandler("onDgsPropertyChange", emailEdit, handlePropertyChange)

-- Button handlers to trigger property changes
addEventHandler("onDgsMouseClick", setNameBtn, function()
    DGS:dgsSetProperty(nameEdit, "text", "John Doe")
end)

addEventHandler("onDgsMouseClick", setEmailBtn, function()
    DGS:dgsSetProperty(emailEdit, "text", "[email protected]")
end)

validateForm()

This example shows onDgsPropertyChange with button properties:

DGS = exports.dgs
local button = DGS:dgsCreateButton(100, 100, 200, 50, "Click Me", false)

-- Monitor button properties
DGS:dgsAddPropertyListener(button, {"alpha", "enabled"})

function handleButtonChange(propertyName, newValue, oldValue)
    outputChatBox("Button property '" .. propertyName .. "' changed from " ..
                 tostring(oldValue) .. " to " .. tostring(newValue))
end
addEventHandler("onDgsPropertyChange", button, handleButtonChange)

-- Test the property changes
setTimer(function()
    DGS:dgsSetProperty(button, "alpha", 128)
    DGS:dgsSetProperty(button, "enabled", false)
end, 2000, 1)

This example shows monitoring multiple properties:

DGS = exports.dgs
local button = DGS:dgsCreateButton(100, 100, 200, 50, "Monitor Me", false)

-- Monitor multiple properties
DGS:dgsAddPropertyListener(button, {"text", "alpha", "visible"})

function handleButtonChange(propertyName, newValue, oldValue)
    outputChatBox("Property '" .. propertyName .. "' changed from " ..
                 tostring(oldValue) .. " to " .. tostring(newValue))
end
addEventHandler("onDgsPropertyChange", button, handleButtonChange)

-- Test changes
setTimer(function()
    DGS:dgsSetProperty(button, "text", "Changed!")
    DGS:dgsSetProperty(button, "alpha", 128)
end, 1000, 1)


[[{{{image}}}|link=|]] Tip: Use dgsRemovePropertyListener to stop monitoring properties when no longer needed

Author: Mohab

See Also

DGS events

General

Check Box

Combo Box

Drag'N Drop

Edit

Grid List

Menu

Selector

Mouse

Radio Button

Switch Button

Tab

Animation

Plugin

Media

Color Picker

QRCode

Remote Image

DGS functions

Client event functions