Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

searching listbox
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;

}



Report this thread to moderator Post Follow-up to this message
Old Post
Stephanie Kroeplin
11-19-04 08:56 AM


Re: searching listbox
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, 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Marc Dashevsky
11-19-04 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PerlTk archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:36 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.