Hi ,
The Tk::Listbox behaves in way so that even if you click on the empty bottom area, the last element in the list gets selected. How can I chanhe this so that if I explicitly click on a element then only it will get selected otherwise not.
In the following code $selectedIndex is coming as 4 if I click on the empty bottom area of the Listbox. By checking the $selectIndex value I want to know whether I have clicked on an element or on the empty area. How can I do that ?
Also according to CPAN :
$listbox->nearest(y)
Given a y-coordinate within the listbox window, this command returns the index of the (visible) listbox element nearest to that y-coordinate.
Please help me how can I then achieve my objective ?
use Tk; my $mw = MainWindow ->new ; my $listFrame = $mw->Frame->pack; my $listBox = $listFrame->Scrolled( 'Listbox', -scrollbars => 'osoe', -selectmode => 'extended', -background => 'white', -height => 10, -width => 10, -selectborderwidth => 0, -relief => 'flat', )->pack; $listBox->insert('end', qw/red yellow green blue grey/); $listBox->bind('<Button-1>', sub { my @elements = $listBox->curselection ; my $rooty = $listBox->rooty; my $y = $Tk::event->Y - $rooty; my $selectedIndex = $listBox->nearest( $y ); my $selectedElement = $elements[$selectedIndex]; print "rooty $rooty \n"; print "elements @elements |selectedIndex $selectedIndex |selectedEleme +nt $selectedElement \n"; } ); MainLoop;
In reply to How to determine if nothing is selected in a listbox by simonz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |