Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting index from listbox

Status
Not open for further replies.

lakshmivaragan

Programmer
Sep 30, 2003
25
IN
Hi,
Is there any inbuilt function to get the index when the text is given ?(Text items are unique)

(At present I'm getting index by my own proc. )

Thanx
Lakshmivaragan
 
Not directly.
The simpler is to set the -listvariable option and lsearch your text inside the list:
listbox .l -listvariable ::mylist
.l insert 0 one two three
proc find {text} { lsearch $::mylist $text }
puts [find two]
-->1

HTH

ulis
 
I agree with ulis that the easiest way to do this is with an associated -listvariable. But if you're using a version of Tcl before the introduction of -listvariable (Tcl/Tk 8.3.0), you can instead retrieve the contents of the listbox with the get operation and then do the search.

[tt]% [ignore]pack [listbox .l][/ignore]
% .l insert 0 First Second Third Fourth Fifth
% .l get 0 end
First Second Third Fourth Fifth
% [ignore]lsearch [.l get 0 end] "Third"[/ignore]
2[/tt]

But even in this situation (pre-8.3), if I were needing to do a "reverse-lookup" of listbox entries frequently, I'd create a "shadow variable" of the listbox contents rather than constantly executing the get operation.

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top