vcldwscript

How to change the caption of a button, using DWScript


I want to interact with GUI elements, from within DWScript (created by Eric Grange). For example change the caption of a button from 'Click Me' to 'Clicked';

I think that within tdwsUnit the TButton class must be declared (under classes), as well as the Caption property. (under properties) And Under Variables a variable with the button name

and in the script the following line : " Button.Caption := 'Clicked'; "

However i stumble a bit on read and write access.

I expected the caption of the button to change, upon running the script.

Can you give a small example.

Thanks in advance Rudolf ter Haar.


Solution

  • Manipulating user interface elements from DWScript isn't as easy as one might think.

    While wrapping an external object (like a button) is easy, controlling and synchronizing the lifetime and ownership of the script-side and Delphi-side objects can be very tricky.

    For example, a TButton in Delphi is usually owned by a form. This means that when the form is destroyed, the button is automatically also destroyed. Now, let's say that you've wrapped the button in a script object. What happens to this script object when the form is destroyed - and the button along with it? The script object doesn't know the button object has been destroyed so any reference to the button will cause an access violation.

    So you need to somehow explicitly handle situations such as this and unfortunately, DWScript doesn't contain a ready-made solution.

    The open-source library linked below is the only publicly available solution known to me (and I'm the author, btw). It contains a pretty extensive encapsulation of the RTL and most common GUI elements. It mostly wraps the DevExpress UI controls, but it will be easy to modify that to wrap the standard VCL controls instead.

    https://bitbucket.org/anders_melander/dwscriptstudio/

    DWScriptStudio

    The script libraries can be found at https://bitbucket.org/anders_melander/dwscriptstudio/src/master/Source/ScriptRTL/

    Since I've wrapped the whole control class hierarchy, the TButton.Caption property setter is actually implemented by the wrapper for TControl.SetText: