ListBox searching......

ListBox searching......

Post by nagandl » Sat, 11 Aug 2007 21:02:41


i,

I am searching with in a list box using keypress event(Ev('A')).
its working properly ..
but my problem is

i am doing two seaches
1.entry button bind search
2.listbox bind search

when i am doing entry button bind search..working properly
if a am doing listbox bind search its also working except cursor
selection in list box......

here i am giving example code for that

please copy and paste the code and check...in the code two subroutines
r there.please comment one by one and check.....

#!/usr/bin/perl
use warnings;
use strict;
use Tk;

my $mw = MainWindow->new();
my $entrywid = undef;
my $srch = undef;

my @list = qw/alpha abcd apple ampear umbrella amber beta charlie
delta echo foxtrot golf hotel
india juliet kilo lima motel nancy oscar papa quebec radio sierra
snake smooth snip
milk boy cat pot cage queen shrek harry tango uniform victor
whiskey xray yankee zulu zoo 20-red 20_white/;

my @search_array = ();
my $sl = $mw->Scrolled('Listbox',
# -takefocus => 1,
-height => 20,
-width => 20,
# -selectmode => "extended",
-selectbackground => 'YELLOW',
-selectforeground => 'black'
)->pack;

if(!Exists($entrywid))
{
$entrywid = $mw->Entry(-textvariable=>\$srch,
-width=>10,-font => ['Arial','12'],
-background => 'white')->pack;

}

$sl->insert( 0, @list );
#$sl->bind( '<KeyPress>', [ \&FindEntry, Ev('A') ] );
$entrywid->bind( '<KeyPress>', [ \&FindEntry_2, Ev('K') ] );
$sl->focus;

MainLoop;

sub FindEntry_2
{
if(!Exists($entrywid))
{
$entrywid = $mw->Entry(-textvariable=>\$srch,`
-width=>10,-font => ['Arial','12'],
-background => 'white')->pack;

}

my ( $en, $key ) = @_;
my $e = $en->XEvent;
my($keysym_t,$keysym_d) = ($e->K,$e->N);

if($keysym_t =~ /BackSpace/)
{
pop(@search_array);
}elsif($keysym_t =~ /Escape/ or $keysym_t =~ /Return/)
{
@search_array =();

$srch = undef;
$sl->focus();
return;
}
else
{
push(@search_array,$key);
}

#$srch = join '',@search_array ;
print "findEntry called $key , $srch , <$keysym_d> <$keysym_t>
\n";

$sl->selectionClear( 0, 'end' );
return unless $srch =~ /\w/;
foreach my $i ( 0 .. ( @list - 1 ) ) {
if ( $list[$i] =~ /^$srch/ ) {
$sl->selectionClear(0,"end");
#$sl->selectionAnchor($i);
$sl->activate($i);

$sl->selectionSet($i);
$sl->see($i);
return;
}
}
}

sub FindEntry {
if(!Exists($entrywid))
{
$entrywid = $mw->Entry(-textvariable=>\$srch,
-width=>10,-font => ['Arial','12'],
-background => 'white')->pack;

}

my ( $sl, $key ) = @_;
my $e = $sl->XEvent;
my($keysym_t,$keysym_d) = ($e->K,$e->N);

if($keysym_t =~ /BackSpace/)
{
$srch = undef;
pop(@search_array);

}elsif($keysym_t =~ /Escape/ or $keysym_t =~ /Return/)
{
@search_array =();
$srch = undef;
return;
}
else
{
push(@search_array,$key);
}

$srch = join '',@search_array ;
print "findEntry called $key , $srch , <$keysym_d> <$keysym_t>
\n";

$sl->selectionClear( 0, 'end' );
return unless $srch =~ /\w/;
foreach my $i ( 0 .. ( @list - 1 ) ) {
if ( $list[$i] =~ /^$srch/ ) {
$sl-&g
 
 
 

ListBox searching......

Post by Jack » Sat, 11 Aug 2007 23:45:03


Why?


What isn't working with the selection? Could you explain further?
I guess I don't understand the problem you are having.


[snip code]

Jack

 
 
 

ListBox searching......

Post by nagandl » Sun, 12 Aug 2007 13:27:42


ok,,

when i am doing listbox searching using listbox bind event the
selection is not coming while using <Up><Down> keys.
wat i have to do to get cursor selection while searching in listbox
with listbox bind subrotine

the subrotine is like this

#!/usr/bin/perl
use warnings;
use strict;
use Tk;

my $mw = MainWindow->new();
my $entrywid = undef;
my $srch = undef;

#my @list = qw(alpha bravo charlie delta echo foxtrot);
my @list = qw/alpha abcd apple ampear umbrella amber beta charlie
delta echo foxtrot golf hotel
india juliet kilo lima motel nancy oscar papa quebec radio sierra
snake smooth snip
milk boy cat pot cage queen shrek harry tango uniform victor
whiskey xray yankee zulu zoo 20-red 20_white/;

my @search_array = ();
my $sl = $mw->Scrolled('Listbox',-height => 20,-width => 20,-
selectbackground => 'YELLOW',-selectforeground => 'black')->pack;

$sl->insert( 0, @list );
$sl->bind( '<KeyPress>', [ \&FindEntry, Ev('A') ] );
$sl->focus;

MainLoop;

sub FindEntry {
if(!Exists($entrywid))
{
$entrywid = $mw->Entry(-textvariable=>\$srch,
-width=>10,-font => ['Arial','12'],
-background => 'white')->pack;

}

my ( $sl, $key ) = @_;
my $e = $sl->XEvent;
my($keysym_t,$keysym_d) = ($e->K,$e->N);

if($keysym_t =~ /BackSpace/)
{
$srch = undef;
pop(@search_array);

}elsif($keysym_t =~ /Escape/ or $keysym_t =~ /Return/)
{
@search_array =();
$srch = undef;
return;
}
else
{
push(@search_array,$key);
}

$srch = join '',@search_array ;
print "findEntry called $key , $srch , <$keysym_d> <$keysym_t>
\n";

$sl->selectionClear( 0, 'end' );
return unless $srch =~ /\w/;
foreach my $i ( 0 .. ( @list - 1 ) ) {
if ( $list[$i] =~ /^$srch/ ) {
$sl->selectionClear(0,"end");
$sl->activate($i);
$sl->selectionSet($i);
$sl->see($i);
return;
}
}
}
 
 
 

ListBox searching......

Post by Rob Seege » Mon, 13 Aug 2007 00:32:47

I'm not 100% on the behavior you expect, but I do know that your binding
effectively prevents Up and Down keys from doing anything because each
time one of those keys is pressed nothing is added to your search
string, so it stays the same, and you clear/reset the selection for the
same item even though it appears that nothing has occurred.

Before your code where you deal with some of the non-letter keys:

if($keysym_t =~ /BackSpace/)

Try adding this:


## Allow the Up/Down bindings to handle up/down
return if $keysym_t =~ /Up|Down/;

if($keysym_t =~ /BackSpace/)


Now, Up or Down keys should have zero affect on your searching. You were
not clear on what you wanted to occur when Up or Down were pressed, but
let's say that we want the Up/Down keys to transfer the value of the
newly selected list element to your entry, You could do something like this:

$sl->bind( '<Up>', \&UpDown);
$sl->bind( '<Down>', \&UpDown);

The following sub should integrate into your code. Yes, the logic could
have been tweaked and moved underneath your FindEntry sub, however it
only adds more complexity and opportunity for problems. Besides, it
doesn't really fit the logic there. If we are moving the cursor, then it
is being moved to an element that we know exists, and we know where it
is, so there is no need to find it, just update the search string and array.

sub UpDown {
my ($sl, $change) = @_;
my ($sel) = $sl->curselection;
if (length($sel) > 0) {
$srch = $sl->get($sel);
@search_array = split(//, $srch);
}
}


Rob