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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.