Copy link to clipboard
Copied
Hello friend!
In my studies I learned how to add or remove items in a dropdown list, but I couldn't find any article that shows how to replace a selected item. How to replace items?
var dlg = new Window("dialog");
dlg.text = "Dialog";dlg.orientation = "column";
var list_array = ["Banana","Orange","Melon"];
var list = dlg.add("dropdownlist", undefined, undefined, {name: "list", items: list_array});
list.selection = 0;
list.preferredSize.width = 100;
var et1 = dlg.add('edittext {properties: {name: "et1"}}');
et1.preferredSize.width = 100;
var b1 = dlg.add("button", undefined, undefined, {name: "b1"});
b1.text = "To replace";
b1.onClick = function(){
var m = "replace " + list.selection.text + " for " + et1.text + " in the drop-down list";
alert (m.toUpperCase())
}
dlg.show();
Insert as first line to b1.onClick:
list.selection.text = et1.text
Copy link to clipboard
Copied
You need to delete all items and then add the new list.
Copy link to clipboard
Copied
replace()
function replace(){
list.remove(list.selection);
list.add("item", et1.text );
list.selection = list.items.length-1;
}
Hi @Chuck Uebele , thanks for the tip, I had tried something like this, it worked 50% desirable, the problem is that the item doesn't stay in the previous order
Copy link to clipboard
Copied
You have to add them all back, one by one, in the order that you want them. It would be nice to be able to insert one, but that doesn't work, as far as I know. I normally put them all back into an array, then create a loop to add them all back one at a time, in order.
Copy link to clipboard
Copied
@Chuck Uebele I confess that I have little knowledge about arrays, I certainly won't be able to do it myself, could you kindly show this in practice using my script above?
Copy link to clipboard
Copied
It would be nice to be able to insert one, but that doesn't work, as far as I know.
By @Chuck Uebele
Pay attention to the third parameter of the list.add() function
Copy link to clipboard
Copied
Third? I found only 2 ('item' and name of that item), but that lets add only, not insert 😞
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You are right for once again and actually like always!
I looked at add() for containerObj.(type [, bounds, text, { creation_props> } ]);
As I didn't notice the 'dropdownlist' note was referring me to other page I didn't go to 😉
Copy link to clipboard
Copied
Insert as first line to b1.onClick:
list.selection.text = et1.text
Copy link to clipboard
Copied
@Kukurykus homem você está cada dia mais espetacular em suas soluções. Sou muito grato a toda esta comunidade, especialmente a você. Grande colaborador. Obrigado
Copy link to clipboard
Copied
You're right, and I'm the LIVING LEGEND! I like this name much more than previous title 😛