Re: NEWT :: selecting an item from a listbox
- Posted by K_D_R Dec 26, 2012
- 2015 views
Thanks, Greg, for the sample code. I am still having problems - here is the code I have been working with, the comments will show variations I have tried.
include ../newt.e include std/machine.e procedure main() -- create a simple list of things sequence fruits = { "Apples", "Bananas", "Oranges" } newtComponent form, listbox, buttonSubmit newt:Init() newt:Cls() newt:CenteredWindow( 64, 24, "*Select Item" ) -- create a new list box listbox = newt:Listbox( 2, 2, 5, NULL ) -- listbox = newt:Listbox( 2, 2, 5, NEWT_FLAG_RETURNEXIT ) -- listbox = newt:Listbox( 18, 3, 10, NEWT_FLAG_SCROLL ) -- listbox = newt:Listbox( 18, 3, 10, NEWT_FLAG_SCROLL ) -- newt:ListboxAppendEntry(listbox, "", 0) -- newt:ListboxAppendEntries(listbox, fruits, 0) -- loop through the items for index = 1 to length( fruits ) do -- allocate some memory atom ptr = allocate( 4, 1 ) -- 1 = automatically free -- store the index of this item poke4( ptr, index ) -- add the item to the list box newt:ListboxAppendEntry( listbox, fruits[index], ptr ) end for buttonSubmit = newt:Button( 22, 20, "Submit" ) form = newt:Form( NULL, NULL, 0 ) newt:FormAddComponents( form, { listbox, buttonSubmit }) --// run the form here //-- newt:FormRun(form) -- get the selected "item" (our pointer above) atom ptr = newt:ListboxGetCurrent( listbox ) -- fetch the index from the pointer integer index = peek4s( ptr ) -- display the result printf( 1, "you selected: %s\n", fruits[index] ) newt:FormDestroy(form) newt:Finished() end procedure main()
This is the ex.err file: /home/ken/euphoria/euprogs/eunewt/demo/fruit_listbox.ex:57 in procedure main() subscript value 32880 is out of bounds, reading from a sequence of length 3 retval (from inlined routine 'Finished' at 213) = <no value> retval (from inlined routine 'Init' at 6) = <no value> fruits = { {65'A',112'p',112'p',108'l',101'e',115's'}, {66'B',97'a',110'n',97'a',110'n',97'a',115's'}, {79'O',114'r',97'a',110'n',103'g',101'e',115's'} } form = 18612064 listbox = 18755504 buttonSubmit = 18620320 index = 3 ptr = <no value> ptr = 16715776 index = 32880 ... called from /home/ken/euphoria/euprogs/eunewt/demo/fruit_listbox.ex:62 Public & Export & Global & Local Variables /home/ken/euphoria/include/std/memconst.e: DEP_really_works = 0 use_DEP = 1 FREE_RID = 2 kernel_dll = <no value> memDLL_id = <no value> VirtualAlloc_rid = <no value> VirtualLock_rid = <no value> VirtualUnlock_rid = <no value> VirtualProtect_rid = <no value> GetLastError_rid = <no value> GetSystemInfo_rid = <no value> /home/ken/euphoria/include/std/memory.e: edges_only = <no value> check_calls = 1 VirtualFree_rid = <no value> /home/ken/euphoria/include/std/types.e: Defined_Sets = { {98'b',99'c',100'd',102'f',103'g',104'h',106'j',107'k', 108'l',109'm',110'n',112'p',113'q',114'r',115's',116't',118'v',119'w',120'x', 121'y',122'z',66'B',67'C',68'D',70'F',71'G',72'H',74'J',75'K',76'L',77'M', 78'N',80'P',81'Q',82'R',83'S',84'T',86'V',87'W',88'X',89'Y',90'Z'}, {97'a',101'e',105'i',111'o',117'u',65'A',69'E',73'I', 79'O',85'U'}, { {48'0',57'9'}, {65'A',70'F'}, {97'a',102'f'} }, {32' ',9,10,13,11,160}, { {32' ',47'/'}, {58':',63'?'}, {91'[',96'`'}, {123'{',126'~'} }, { {32' ',126'~'} }, { {32' ',126'~'}, {32' ',32' '}, {9,9}, {10,10}, {13,13}, {8,8}, {7,7} }, { {97'a',122'z'} }, { {65'A',90'Z'} }, { {48'0',57'9'}, {97'a',122'z'}, {65'A',90'Z'} }, { {48'0',57'9'}, {97'a',122'z'}, {65'A',90'Z'}, {95'_',95'_'} }, { {97'a',122'z'}, {65'A',90'Z'} }, { {0,127} }, { {0,31}, {127,127} }, { {48'0',57'9'} }, { {33'!',126'~'} }, { {0,255} }, {95'_'}, {1,0} } /home/ken/euphoria/include/std/machine.e: FREE_ARRAY_RID = 1 page_size = 4096 /home/ken/euphoria/euprogs/eunewt/demo/../newt.e: libnewt = 16605808
Any suggestions will be appreciated!
Kennneth Rhodes