sandeep.ses has asked for the wisdom of the Perl Monks concerning the following question:

i have a list and i am trying to check if any entry/row has been selected or not. if no entry is selected i want to select all the entries. i can see that there is a option to selectSet($frm , $to) in my case it would be from 0 to no of rows. is there a way to select all entries or is there a way to find out no of entries in the list. without having to keep track of the path for the last entry. Sandeep

Replies are listed 'Best First'.
Re: perl/tk HLIST selections options
by SciDude (Friar) on Jul 09, 2004 at 04:21 UTC

    Selecting everything is simple enough:

    # select everything $lb->selectionSet(0, 'end' );

    But, you only want to do this if no entry is selected.

    @selected = $lb->curselection; unless (@selected) { $lb->selectionSet(0, 'end' ); } foreach (@selected) { # do something with the index in $_ }

    Untested!


    SciDude
    The first dog barks... all other dogs bark at the first dog.
      selectionSet(0, 'end' ); dint seem to work it gives me an error entry end not found

        Looking around some more, you may be able to get at the "end" value:

        $listbox->index(index) Returns the integer index value that corresponds to index. If inde +x is end the return value is a count of the number of elements in the + listbox (not the index of the last element).

        Really, really untested code. Good luck!


        SciDude
        The first dog barks... all other dogs bark at the first dog.
Re: perl/tk HLIST selections options
by eserte (Deacon) on Jul 09, 2004 at 09:26 UTC
    To get the last element, something like this could work (untested code!):
    $last_entry = ($hlist->info("children"))[-1];
Re: perl/tk HLIST selections options
by zentara (Cardinal) on Jul 09, 2004 at 14:30 UTC
    There is something to watch out for in HList selections. There is an "internal counter in HList" which increments the selection number of the list children. For instance, if you have 10 list entries, their list numbers will be 0-9. If you delete them, and make 10 new entries, the last entry number will now be 19.

    So you have to test for the existence of the selection. For example, I wanted to delete an entry, and then have the active selection move to the next entry. If the next entry dosn't exist, go to the previous entry.Well you may have already deleted the "previous internal number" and you will get "entry dosn't exist" if you just try to go to the previous(or next) number. So you need something along the line of this sub. It just gives you the idea.

    sub{my $listnum =$h->info('selection'); $h->delete('entry',$h->info('selection' +)); if($h->info('exists', $listnum + 1)) +{ $h->selectionSet($listnum + 1) }else{ while($listnum--){ if($h->info('exists',$ +listnum)){ $h->selectionSet($ +listnum);last} } } delete $info{$key_sel};

    I'm not really a human, but I play one on earth. flash japh