in reply to How to move up/down the row element in a ListBox/Mlistbox
Binding to the up and down arrow keys is somewhat problematic because they are already used for list box navigation. However using 'x' and 'e' for the down and up keys you can reorder the list as follows:
#!usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $lb = $mw->Scrolled("Listbox", -scrollbars => 'e',)->pack( -side => 'left', -fill => 'y' ); for my $key ("first", "second", "third", "fourth") { $lb->insert('end', $key); } $lb->activate(0); $lb->selectionSet(0); $lb->focus(); $lb->bind('<x>', sub {down($lb, @_)}); $mw->bind('<e>', sub {up($lb, @_)}); MainLoop; sub down { my ($lb) = @_; my @selItems = $lb->curselection(); return if 1 != @selItems || $selItems[0] == $lb->size(); my $item = $lb->get($selItems[0]); $lb->delete($selItems[0]); $lb->insert($selItems[0] + 1, $item); $lb->selectionSet($selItems[0] + 1); } sub up { my ($lb) = @_; my @selItems = $lb->curselection(); return if 1 != @selItems || $selItems[0] == 1; my $item = $lb->get($selItems[0]); $lb->delete($selItems[0]); $lb->insert($selItems[0] - 1, $item); $lb->selectionSet($selItems[0] - 1); }
|
|---|