12. Listbox (multiple selection)

If you are giving the user a small list of choices then stick with the checkbox. However, use the listbox for long lists. An user can select one or more options.

mainwindow


use Prima qw(Application Themes Buttons Label ComboBox);

require "/home/reinier/mcMessage.pl"; # code 7. Messagewindow

$mw = Prima::MainWindow-> create(
      origin  => [100, 100],
      size    => [300, 200],
      designScale => [7, 16], # width and height of the font when designed, transformed in proportion between the designed
                                    and the actual font metrics, to avoid garbled windows on different systems
      text => "Listbox",
      icon => Prima::Icon-> load('icon.png'),	
      skin => 'flat',
);

$lb = $mw-> insert( "ListBox",
      name           => 'ListBox1',
      origin         => [50, 80],
      size           => [200, 100],
      multiSelect    => 1, # if 0, the user can select only one item.
      extendedSelect => 1, # if 1, the user can drag mouse or use Shift key plus arrow keys to perform range selection; the Control key can be used to select individual items. 
      font           => { size => 12},
      items          => [ 'Black', 'Blue', 'Brown', 'Cyan', 'Green' ],
      align          => ta::Left,

);
				          
$mw-> insert('Prima::Button' =>
              origin => [100, 30],
              size => [100, 30],
              text => 'Evaluate', 
              onClick => sub { 

                         $mcSelectedOptions = "";
				          
                         foreach (@{ $lb-> selectedItems }){

                            if ($mcSelectedOptions eq "") {
                              $mcSelectedOptions = $lb->items->[$_];
                               # alternative: $mcSelectedOptions = $lb->{items}[$_];
                               # alternative: $mcSelectedOptions = $lb->{items}->[$_];                                                              
                            }
                            else {
                                  $mcSelectedOptions = join (" - ", $mcSelectedOptions, $lb->items->[$_]);		
                                   # alternative: $mcSelectedOptions = join (" - ", $mcSelectedOptions, $lb->{items}[$_]);
                                   # alternative: $mcSelectedOptions = join (" - ", $mcSelectedOptions, $lb->{items}->[$_]);                                  					  
                            }
                         };

                         mcMessage(400, 100, "Selected option(s)", "Selected option(s): " . $mcSelectedOptions); 
              }
);


run Prima;