Re: Set selection item for listbox



Gerald W. Lester wrote:
Kevin Walzer wrote:
I want to search a listbox for a term and set the selection to that term:

listbox .l -listvariable foo
pack .l

set foo "John"

set listitem [list John Paul George Ringo]

foreach item $listitem {
.l insert end $item
}

.l selection set [lindex [lsearch -inline [.l get 0 end] "John"]]

This code returns this error:

Error in startup script: bad listbox index "John": must be active,
anchor, end, @x,y, or a number
while executing
".l selection set [lindex [lsearch -inline [.l get 0 end] "John"]]"

Can anyone point me in the right direction?


Try executing the following commands interactively and see if you see
what is wrong:
% set listitem [list John Paul George Ringo]
% lsearch -inline $listitem "John"
% lindex John


That is for starters. Looking at your code, I think you should reread
the listbox, lindex and lsearch man/help pages.


I solved the problem with this additional code:

set newlist [.l get 0 end]
set newindex [lsearch -exact $newlist Ringo]
..l selection set $newindex
..l selection anchor $newindex
..l see $newindex

Thanks.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
.