Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Tk Listbox problem

by Grygonos (Chaplain)
on Jul 21, 2003 at 17:07 UTC ( [id://276356]=perlquestion: print w/replies, xml ) Need Help??

Grygonos has asked for the wisdom of the Perl Monks concerning the following question:

I have a listbox setup like so...
#Create the tape removal dialog box my $remove_dialog = $win->DialogBox(-title=>'Choose a tape', -buttons=>["Remove", "Cancel"] +); #Create the listbox to hold the tape numbers my $remove_list = $remove_dialog->Listbox(-width=>'25', -height=>'5', -font=>$font)->pack; #Get the tapes that are stored my @stored_tapes = getTapes(); #Put each tape in the listbox foreach my $tape (@stored_tapes) {$remove_list->insert('end',$tape);} #Show the $remove_dialog box to the user my $remove_result = $remove_dialog->Show; if($remove_result eq "Remove") { my $test = $remove_list->get('active'); print $test; }
This works fine. The problem is ... when I click Remove with nothing selected in the box
$remove_list->get('active')
defaults to the first value in the list. I would like to be able to check for the case of the user not selecting an option.

Does anyone know how this is possible?

Thanks for any help, Gry

Replies are listed 'Best First'.
Re: Tk Listbox problem
by jsprat (Curate) on Jul 21, 2003 at 17:52 UTC
    Check the return of $remove_list->curselection. curselection returns the number of items selected or an empty string for no selection.

    perldoc Tk::Listbox has more information.

    HTH...

Re: Tk Listbox problem
by bobn (Chaplain) on Jul 21, 2003 at 17:32 UTC

    From perldoc Tk::Listbox, I can only come up with:

    foreach my $tape (@stored_tapes, '<NONE>') {$remove_list->insert('end',$tape);} $remove_list->activate('end'); #Show the $remove_dialog box to the user my $remove_result = $remove_dialog->Show;

    In the absence of selection, returns '<NONE>'. You might want to set -selectmode' to multiple or extended to allow more than one selection at a time.

    --Bob Niederman, http://bob-n.com
      clarification of my previous post.. I want the script to throw an error or prompt for proper input when a user fails to make a selection in the listbox.
      Edit: Thanks for the curselection tidbit. Does anyone know of a better way of reading perldoc on win32... dos shell blows.

        If Show returns '<NONE>' prompt the user again, thought the curselection() method noted below does look a bit more elegant.

        Also, on my winNT AS 5.6.1 system, perldoc works, if I recall (don't have it here right now). In any event, there shpould be a an item in Start->Programs for ActiveState Perl Documentation, or somethng to that effect.

        --Bob Niederman, http://bob-n.com
        Tk is a standard module with ActiveState, and AS ships HTML docs. Mine is here: D:/Perl/html/site/lib/Tk/Listbox.html. If you're online, try perldoc.com

        That being said, I installed less for windows, changed the background color of my command prompt. Except for the OLE object browser, I don't use the HTML docs any more.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://276356]
Approved by crouchingpenguin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-29 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found