3

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.

RobC
  • 22,977
  • 20
  • 73
  • 80
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125

2 Answers2

3

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.

pdizz
  • 4,100
  • 4
  • 28
  • 42
  • The script you supplied doesn't toggle the radio buttons; which is what I was hoping for. – Ghoul Fool Nov 16 '12 at 16:18
  • I didn't see that in the question. What do you mean by toggling? The default behavior of the radio buttons (selecting one toggles off all others) should automatically apply to all buttons within the same grouping. Did you have something else in mind? – pdizz Nov 16 '12 at 16:31
  • Sorry, I thought that was obvious. I've added another code example to the initial question. – Ghoul Fool Nov 16 '12 at 16:42
  • Are you trying to turn one button on initially by default? Is this what you're having trouble with `win.radiobutton0.value = true;`? – pdizz Nov 16 '12 at 17:22
  • Note: If your radio buttons are not next to each other, for example, say you put a line between them, then they become part of a different group. For example, when you do that you would have two radio buttons selected at one time. At least that's how it worked in my script. I was looking for a way to make all the radio buttons apart of the same group but removing the "inbetween" elements works. – 1.21 gigawatts Jan 04 '16 at 06:21
  • Link is down unfortunately – derHugo Oct 12 '18 at 07:52
  • is it possible to asign an ID to each radio button? – Kamel Labiad Feb 04 '23 at 10:36
0

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
}
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125