Assume I have
combo(["A","B","C","D"])
How can I select item B. Is there a property like selectIndex(2) or selectItem("B") ?
I could not find such property.
Assume I have
combo(["A","B","C","D"])
How can I select item B. Is there a property like selectIndex(2) or selectItem("B") ?
I could not find such property.
I will assume you have a datatype declared like this:
data C = combo(list[str] items);
and a value like this (to line up with your question):
rascal>c = combo(["A","B","C","D"]);
C: combo(["A","B","C","D"])
Given that, there are several ways you can get at the second item in the list. If you have given a field name to the data held in combo
(above, this is items
), you can just say this (remembering that lists in Rascal are 0-indexed):
rascal>c.items[1];
str: "B"
If you haven't given it a name, and don't want to, you can also do this, using a match:
rascal>if (combo(l) := c) println(l[1]);
B
The variable l
will be bound to the list held inside combo
; this new variable is then available inside the if
. Of course, in both of these cases you would want to make sure the list is long enough, so you don't get an error trying to read the second element. You could also do the following, which uses a nested list match:
rascal>if (combo([s1,s2,s*]) := c) println(s2);
B
In this case, s1
will be found to the first element of the list, s2
to the second, and s
to anything (0 or more, which is what the *
means) that follows. Since this will only match if the list held in combo
is at least length 2, you wouldn't need to separately check for this.
Looks like the first entry is hard-wired to be selected by default: https://github.com/cwi-swat/rascal/blob/master/src/org/rascalmpl/library/vis/figure/interaction/swtwidgets/Combo.java#L40
So the only way to select the item you want is to create the combo with the first item the one you want selected.