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!

variable with istboxselect

Status
Not open for further replies.

RJHoldi

Technical User
Nov 7, 2003
15
DE
hi ng,

i try to get a variable in bonding with listboxselect. i got a list shown in entry1 and the result schould be shown in entry2. i get an eror that the there is no such variable xyz. here is the code:

set list1 {1 2 3 4 5 6 7 8 9}
set list2 {a b c d e f g h i}

listbox .listbox
set list1_lenght [llength $list1]
for {set i 0} {$i <= $list1_lenght} {incr i} {
.listbox insert end [lindex $list1 $i]
}


entry .result -width 16 -relief sunken -textvariable result
pack .listbox .result
bind .listbox <<ListboxSelect>> {set xyz [selection get]} # here the variable xyv is set
set index [lsearch $list1 $xyz] # here the error occurs
set result [lindex $list2 $index]


why is the variable xyz not set ?
regards Reinhold
 
Right off the bat, there's a really cool shortcut to get a list associated with a listbox. Use the -listvariable option:
listbox .listbox -listvariable list1
By the way, it's probably considered bad form to name a listbox, "listbox".
So, now you won't need:
for {set i 0} {$i <= $list1_lenght} {incr i} {
.listbox insert end [lindex $list1 $i]
}


Now, selection get is really [red].listbox[/red] selection get, only there is no ... selection get. What you want, I think, is .listbox curselection.

So I would do it this way:
Code:
set list1 {1 2 3 4 5 6 7 8 9}
set list2 {a b c d e f g h i}

listbox .listbox [red]-listvariable list1[/red]
entry .result -width 16 -relief sunken -textvariable result
pack .listbox .result
bind .listbox <<ListboxSelect>> {set xyz [.listbox curselection]}  # here the variable xyv is set
set result [lindex $list2 $xyz]




_________________
Bob Rashkin
 
hi bob,
thanks for your answer. the hints with listvariable and curselection are super. but the variable xyz still is not set.
regards Reinhold
 
It seems to work for me. In a WISH session I did this:
Code:
% set list1 {a b c d e}
a b c d e
() 2 % pack [listbox .lb -listvariable list1]
() 3 % bind .lb <<ListboxSelect>> {set ix [.lb curselection]}
() 4 % set ix
3
() 5 %
between lines 3 and 4, I selected "d" in the listbox, and indeed, "ix" was set to "3".

_________________
Bob Rashkin
 
hi bob,
you are right. i tried to run it from a script and so the var is not set. i just have to bind the "set ix" to an event and that it should be.

thanks for help and i really need an other book,
regards Reinhold
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top