How to convert a Tcl numeric pointer to its element
I feel like this question has a simple answer; but, for the life of me, I could not figure it out. I am trying to convert a list selection to my string element so that I can enter it into the database.
I understand what I can use. listbox curselection to get its index; however I need to convert it to string. Can anyone help me with this?
Thank,
DFM
source to share
Here's a simple, working example ...
proc selectionMade {w} {
# --- loop through each selected element
foreach index [$w curselection] {
puts "Index --> $index"
puts "Text --> [$w get $index]"
}
}
catch {console show}
listbox .lb
bind .lb <<ListboxSelect>> {selectionMade %W}
pack .lb -fill both
.lb insert end "Line 1"
.lb insert end "Line 2"
So [.lb curselection] returns a list of indices of the selected items. To turn the index into the actual text of an element, you simply need to use it with the [.lb get $ index] subcommand as shown above.
source to share
You should pick up a copy of Practical Programming in tcl and tk . I am "The Book of Camels" (to steal the perl idiom) tcl / tk.
As for your question, then you want:
set selectedText [list]
foreach selectedLine [$listbox curselection] {
lappend selectedText [$listbox get $selectedLine ]
}
source to share