Question

Having problems implementing radio buttons. I know radio buttons in CS2 can be problematic but I'm not sure where I am going wrong. I suspect I have a bracket or comma in the wrong place; but can't see it. Thank you.

var dlg =
"dialog {text:'Script Interface',bounds:[100,100,300,260]," +
"info: Group { orientation: 'column', alignChildren: 'center'," + 
"radiobutton0:RadioButton {bounds:[50,30,150,40] , text:'layerName0', alignment: 'left' }," +
"radiobutton1:RadioButton {bounds:[50,50,150,90] , text:'layerName1', alignment: 'left'  }}" +
"cancelBTN:Button{bounds:[110,130,190,150] , text:'Cancel' },"+
"processBTN:Button{bounds:[10,130,90,150] , text:'Ok' }}";
var win = new Window(dlg,"radio buttons"); 
win.radiobutton0.value = true;
win.center(); 
win.show();

Another thing: Is there a better way of writing UI elements as this format is rather ugly.

Here's the bare bones code that works. var dialogBox = "dialog { orientation: 'column', alignChildren: 'center', \ info: Group { orientation: 'column', alignChildren: 'center', \ rbtn1: RadioButton { text: 'Radio Button 1', align: 'left'}, \ rbtn2: RadioButton { text: 'Radio Button 2', align: 'left'}, }, }, \ } }";

win = new Window (dialogBox);
win.center(); 
win.show();

I think the radio button toggle is controlled by line 3 as commenting it out stops the radio buttons working correctly.

Was it helpful?

Solution

When I ran the code it threw an error on this line win.radiobutton0.value = true; Object is undefined. This is because the way you have the dialog structured the button is part of the info group within the window. The line should read

win.info.radiobutton0.value = true;

This should toggle radiobutton0 on initially.

You don't have to use a resource string to craft the dialogs if you don't want. Individual elements can be added by creating a reference to the window object (or to a pallate or panel) and using .add()

For example:

var w = new Window ("dialog");
w.alignChildren = "left";
var radio1 = w.add ("radiobutton", undefined, "Radio Button 1");
var radio2 = w.add ("radiobutton", undefined, "Radio Button 2");
radio1.value = true;
w.show ();

This is the most thorough reference on ScriptUI that I've found.

OTHER TIPS

Here's a nicer additive version of a dialogue box with radio buttons working as I want them to.

var w = new Window ("dialog");
w.alignChildren = "left";
var myButtonGroup = w.add ("group");
myButtonGroup.orientation = "column";
myButtonGroup.alignment = "left";
var radio1 = myButtonGroup.add ("radiobutton", undefined, "Radio Button 1");
var radio2 = myButtonGroup.add ("radiobutton", undefined, "Radio Button 2");
myButtonGroup.add ("button", undefined, "OK");
myButtonGroup.add ("button", undefined, "Cancel");
radio1.value = true;
w.center(); 
// w.show ();

if (w.show() == 1)
{
    alert("You picked " + youSelected(myButtonGroup))
}

function youSelected(rButtons)
{
    if (radio1.value == true)
    // radio1 selected
    return radio1.text
    else
    // radio2 selected
    return radio2.text
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top