user-interfacedialogradio-buttonradiobuttonlistdm-script

How do I use the changed method of a dialog radiobutton in dm script


How do you use the radio button chanded method callback in dm script?


The following code is the example code provided by the documentation extended with two radio buttons. The resulting dialog is shown below.

Test Dialog

Clicking the Button works perfectly fine. The Results tab shows "button callback". But when changing the Radio buttons nothing happens. After clicking OK I get an error which sais (as many times as I changed the radio buttons) that the given method does not exist.

How do I use the radio button callback?

class testDialog : UIFrame{
    void buttonCallback(object self){
        result("button callback\n");
    }

    void radioCallback(object self){
        result("radio callback\n");
    }
}

TagGroup dialog_items;
TagGroup dialog_tags = DLGCreateDialog("Test Dialog", dialog_items);

TagGroup button_tag = DLGCreatePushButton("Button", "buttonCallback");
dialog_items.DLGAddElement(button_tag);

TagGroup radio_list = DLGCreateRadioList(0, "radioCallback");
radio_list.DLGAddRadioItem("Radio 1", 0);
radio_list.DLGAddRadioItem("Radio 2", 1);
dialog_items.DLGAddElement(radio_list);

Object dialog = alloc(testDialog).init(dialog_tags);

dialog.Pose();

Solution

  • The callback for the radio button change method needs a TagGroup as second argument:

    void radioButtonChanged(object self, TagGroup radio_list)
    

    So changing the radioCallback() function in the code above to have those two parameters the example works fine (code provided below).

    Note that this makes it easy to get the selected value of the radio buttons because the radio_list contains the value in the "Value" index:

    void radioCallback(object self, TagGroup radio_list){
        number value;
        radio_list.TagGroupGetTagAsNumber("Value", value);
    
        result("radio callback, radio list has value " + value + "\n");
    }
    
    

    The full working example of the code in the question is the following:

    class testDialog : UIFrame{
        void buttonCallback(object self){
            result("button callback\n");
        }
    
        void radioCallback(object self, TagGroup radio_list){
            result("radio callback\n");
        }
    }
    
    TagGroup dialog_items;
    TagGroup dialog_tags = DLGCreateDialog("Test Dialog", dialog_items);
    
    TagGroup button_tag = DLGCreatePushButton("Button", "buttonCallback");
    dialog_items.DLGAddElement(button_tag);
    
    TagGroup radio_list = DLGCreateRadioList(0, "radioCallback");
    radio_list.DLGAddRadioItem("Radio 1", 0);
    radio_list.DLGAddRadioItem("Radio 2", 1);
    dialog_items.DLGAddElement(radio_list);
    
    Object dialog = alloc(testDialog).init(dialog_tags);
    
    dialog.Pose();