For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > November 2004 > searching listbox









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author searching listbox
Stephanie Kroeplin

2004-11-19, 3:56 am

Hello, I have a chunk of code that has a listbox and an entry widget. The
user types something in the entry box and it searches thru the listbox and
makes the closest match active. The searching part seems to work fine, but
I would like the entry widget to display the matched listbox item when the
user presses the enter key. I would settle for another key--perhaps space?
please help me out on this one!!!

#!/work/dchiles/bin/perl

use Tk;

$mw = MainWindow->new;

$mw->title("Listbox");

# For example purposes, we'll use one word for each letter

@choices = qw/alpha beta charlie delta echo foxtrot golf hotel india

juliet kilo lima motel nancy oscar papa quebec radio sierra

tango uniform universal unisex under victor whiskey xray xenon xoi yankee
zulu/;

# Create the Entry widget, and bind the do_search sub to any keypress

$entry = $mw->Entry(-textvariable => \$search)->pack(-side => "top",

-fill => "x");

$entry->bind("<KeyPress>", [ \&do_search, Ev("K") ]);

# Create Listbox and insert the list of choices into it

my $lb = $mw->Scrolled("Listbox", -scrollbars => "osoe",

)->pack(-side => "left");

$lb->insert("end", sort @choices);

$mw->Button(-text => "Exit",

-command => sub { exit; })->pack(-side => "bottom");

MainLoop;

# This routine is called each time we push a keyboard key.

sub do_search {

my ($entry, $key) = @_;

# Ignore the backspace key and anything that doesn't change the word

# i.e. The Control or Alt keys

return if ($key =~ /backspace/i);

return if ($oldsearch eq $search);

print "the key that was pressed = $key\n";

if ($key=~ /space/)

{

$search = $lb->get('active');

print"the active selection is: $search";

}

# Use what's currently displayed in Listbox to search through

# This is a non-complicated in order search

my @list = $lb->get(0, "end");

foreach (0 .. $#list) {

if ($list[$_] =~ /^$search/) {

$lb->see($_);

$lb->selectionClear(0, "end");

$lb->selectionSet($_);

last;

}

}

$oldsearch = $search;

}


Marc Dashevsky

2004-11-19, 3:56 am

In article <OBbnd.15$rw1.1728@news-west.eli.net>, stephanie.kroeplin@office.xerox.com
says...
> Hello, I have a chunk of code that has a listbox and an entry widget. The
> user types something in the entry box and it searches thru the listbox and
> makes the closest match active. The searching part seems to work fine, but
> I would like the entry widget to display the matched listbox item when the
> user presses the enter key. I would settle for another key--perhaps space?
> please help me out on this one!!!


$search = $lb->get($lb->curselection);

> #!/work/dchiles/bin/perl
>
> use Tk;
>
> $mw = MainWindow->new;
>
> $mw->title("Listbox");
>
> # For example purposes, we'll use one word for each letter
>
> @choices = qw/alpha beta charlie delta echo foxtrot golf hotel india
>
> juliet kilo lima motel nancy oscar papa quebec radio sierra
>
> tango uniform universal unisex under victor whiskey xray xenon xoi yankee
> zulu/;
>
> # Create the Entry widget, and bind the do_search sub to any keypress
>
> $entry = $mw->Entry(-textvariable => \$search)->pack(-side => "top",
>
> -fill => "x");
>
> $entry->bind("<KeyPress>", [ \&do_search, Ev("K") ]);
>
> # Create Listbox and insert the list of choices into it
>
> my $lb = $mw->Scrolled("Listbox", -scrollbars => "osoe",
>
> )->pack(-side => "left");
>
> $lb->insert("end", sort @choices);
>
> $mw->Button(-text => "Exit",
>
> -command => sub { exit; })->pack(-side => "bottom");
>
> MainLoop;
>
> # This routine is called each time we push a keyboard key.
>
> sub do_search {
>
> my ($entry, $key) = @_;
>
> # Ignore the backspace key and anything that doesn't change the word
>
> # i.e. The Control or Alt keys
>
> return if ($key =~ /backspace/i);
>
> return if ($oldsearch eq $search);
>
> print "the key that was pressed = $key\n";
>
> if ($key=~ /space/)
>
> {
>
> $search = $lb->get('active');
>
> print"the active selection is: $search";
>
> }
>
> # Use what's currently displayed in Listbox to search through
>
> # This is a non-complicated in order search
>
> my @list = $lb->get(0, "end");
>
> foreach (0 .. $#list) {
>
> if ($list[$_] =~ /^$search/) {
>
> $lb->see($_);
>
> $lb->selectionClear(0, "end");
>
> $lb->selectionSet($_);
>
> last;
>
> }
>
> }
>
> $oldsearch = $search;
>
> }
>
>
>


--
Go to http://MarcDashevsky.com to send me e-mail.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com