Note that I've added hash %is_selected for the Checkbuttons to have a 'memory' ( $is_selected{$item} will tell you whether the given item is required ). The moving of Buttons is achieved with manipulating the list itself (no hash necessary for that):
use Tk; my @items=qw(State Dependency CalcTime Result FooState FooDependency F +ooCalcTime FooResult BarState BarDependency BarCalcTime BarResult); my %is_selected; my $mw=new MainWindow; my $item_width=15; my $UP=1; my $DOWN=-1; my %attr_item=('-background'=>'#ffafaf'); my %attr_btn=('-background'=>'#afffaf','-activebackground'=>'#7aaa7a') +; my $text=$mw->Scrolled('Text', -insertontime=>0, -scrollbars=>'e', -width=>($item_width+15), -background=>'#efefef' )->pack; my $first=1; $text->tagConfigure('item',%attr_item); &build; MainLoop; sub build { $text->delete('1.0','end'); foreach my $item (@items) { my $btn=$text->Checkbutton(%attr_item, -variable=>\$is_selected{$item}, -onvalue=>1, -offvalue=>0, '-activebackground'=>'#ff7f7f' ); my $up=$text->Button(-text=>'Up',-command=>[\&move,$item,$UP],%att +r_btn); my $down=$text->Button(-text=>'Down',-command=>[\&move,$item,$DOWN +],%attr_btn); $text->insert('1.0',"\n") unless $first; $first=0; $text->windowCreate('1.0',-window=>$up); $text->windowCreate('1.0',-window=>$down); $text->windowCreate('1.0',-window=>$btn); $text->insert('1.0',sprintf('%-'.$item_width.'s',$item)); $text->tagAdd('item','1.0',"1.$item_width"); } } sub move { my $what=shift; my $dir=shift; my ($index)=grep {$items[$_] eq $what} (0 .. $#items); my $new_index=$index+$dir; $new_index=0 if($new_index>=@items); $new_index=@items-1 if $new_index<0; my $temp=$items[$new_index]; $items[$new_index]=$items[$index]; $items[$index]=$temp; &build; }

In reply to Re^7: Insert checkbutton into MListbox by chessgui
in thread Insert checkbutton into MListbox by ghosh123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.