Version 29 of Adding 'history' to BWidget ComboBox widgets.

Updated 2006-04-16 16:43:33

WJG (10th June, 2005) The BWidget ComboBox have a 'built-in' means of updating its entry widget, so here a way of doing it.

 # -------------------------------------------------------------
 # ComboBox_history.tcl
 # Written by William J Giddings, 2005
 # -------------------------------------------------------------
 #
 # Purpose:
 # --------
 # To provide history for the BWidget ComboBox.
 #
 # Note:
 # -----
 # * This is not achieved by manipulating the BWidget code but by
 # simply providing a suitable procedure to be called when the
 # entry subwidget is validated.
 # * New validated entries are placed at the top of the displayed list.
 # * Duplicate entries will not be added.
 #
 # Use:
 # ---------------
 # ComboBox .cmb -values {str1 str2..} -command _h
 #
 # -------------------------------------------------------------

 # -------------------------------------------------------------
 # a simple handler, no args required
 # -------------------------------------------------------------

 proc cmb_history {} {
    set w [winfo parent [focus]]
    set a [$w get]
    set b [$w list get 0 end]
    #check to see if value already there..
    if { [lsearch -exact $b $a] == -1} { 
      $w list insert 0 $a
    }
 }

 proc show {args} {
  cmb_history
  puts $args
 }

 # -------------------------------------------------------------
 # the ubiquitous demo!
 # -------------------------------------------------------------
 proc demo {} {
    package require BWidget
    pack [ComboBox .cmb -values {{She sells} {sea shells} {by the} {sea shore} } -command _h]
 }

 demo

See also Enhancing BWidget