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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace listbox element 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Since I couldn't find a replace option in listbox (Tcl/tk 8.4) I started writing my own using:

#find position in list
set position [lsearch $list "Line"]
# replace element from list
$list delete $position
$list insert $position "LINEID"

My list contains
Line
Trace
X
Y
Z

But was this does is to insert LINEID before Line like
LINEID
Line
Trace
...

The other problem is that I have configured each item of the list with a bkg and fg color and I want to keep those. All I want to do is to replace the text of one element with another one.

Any ideas?

Many thanks!
 
Presumably, your -listvariable is "list" in your listbox setup. Would "linsert" work?
linsert list index element ?element element ...?


DESCRIPTION
This command produces a new list from list by inserting all of the element arguments just before the indexth element of list. Each element argument will become a separate element of the new list. If index is less than or equal to zero, then the new elements are inserted at the beginning of the list. If index has the value end, or if it is greater than or equal to the number of elements in the list, then the new elements are appended to the list. end-integer refers to the last element in the list minus the specified integer offset.

You could do something like:
Code:
set list [linsert $list 0 LINEID]

Bob Rashkin
rrashkin@csc.com
 
Looks like the lsearch command returned -1. That is: the value "Line" was not found in $list.

I guess you thought that $list holds your list. In fact, $list holds the path to your widget and can be used to manage this widget. It doesn't handle your list.
The command $list delete $position means: tell the listbox to manage the items for me and delete the item whose position is $position. Its very different to manage directly the list of the items.
And with the lsearch $list "Line" command you said: search the value "Line" in the path of my widget considered as a list. So you got a -1 result indicating that "Line" was not found.

As Bong suggested you should use the -listvariable option to be able to manage the items of the listbox:
Code:
  listbox .lb -listvar ::items
  pack .lb
  set ::items [list]
  lappend ::items Line Trace X Y Z
  set pos [lsearch -exact $::items Line]
  if {$pos > -1}   { set ::items [lreplace $::items $pos $pos LINEID] }
HTH

ulis
 
Hi ulis,

You were right, your solution worked fine, thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top