Code Comments
Programming Forum and web based access to our favorite programming groups.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;
}
Post Follow-up to this messageIn 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, bu
t
> 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.