From: "Enrico Schwass" <deckard73 / freenet.de>
Subject: Rubytk - How to use <<ListboxSelect>>
Date: Mon, 13 Jun 2005 04:06:24 +0900
Message-ID: <opssml5jeqfoz7gx / x5.linux.net>
> In the tk reference manual i found an event called ListboxSelect.
    (snip)
> Could you post a code snippet to demonstrate this callback functionality

How about the following?
--------------------------------------------------
require 'tk'

f = TkFrame.new.pack(:fill=>:x)

entry = TkEntry.new(f).pack(:side=>:left, :expand=>true, :fill=>:x)

b_add = TkButton.new(f, :text=>'ADD', :state=>:normal).pack(:side=>:right)
b_edit = TkButton.new(f, :text=>'EDIT', :state=>:disabled).pack(:side=>:right)

available = b_add
entry.bind('Return', proc{available.invoke})

lbox = TkListbox.new(:selectmode=>:multiple).pack(:fill=>:both, :expand=>true)

old_sel = []
lbox.bind('<ListboxSelect>'){
  sel = (lbox.curselection - old_sel)
  if sel.empty?
    entry.value = ''
    b_add[:state]  = :normal
    b_edit[:state] = :disable
    available = b_add
  else
    lbox.selection_clear(old_sel[0]) unless old_sel.empty?
    entry.value = lbox.get(sel[0])
    b_add[:state]  = :disable
    b_edit[:state] = :normal
    available = b_edit
  end
  old_sel = sel
}

b_add.command{
  unless (val = entry.value).empty?
    lbox.insert(:end, val)
    entry.value = ''
  end
}

b_edit.command{
  unless (val = entry.value).empty?
    idx = lbox.curselection[0]
    lbox.delete(idx)
    lbox.insert(idx, val)
    lbox.selection_set(idx)
  end
}

entry.focus

Tk.mainloop
--------------------------------------------------
-- 
Hidetoshi NAGAI (nagai / ai.kyutech.ac.jp)