请问如何用updown 来控制checklistbox 里面的条目上下移动?
比如checklistbox里面有
1
2
3
4
5
6
想做到选择一个条目比如是4,然后点updown来控制4条目的上下移动.
变成
1
4
2
3
5
6

解决方案 »

  1.   

    procedure MoveBy(moves:Integer); //moves为负表上多,为正表下移
    var
     id:integer;
    begin
    if moves=0 then exit;//不移动
    if checklistbox1.itemindex=-1 then exit; //检查有没有行被选取
    id:=checklistbox1.itemindex;
    if moves<0 then //上移
     if id+moves<0 then moves:=-id; //最上行
    if moves>0 then //下移
     if id+moves>checklistbox1.count then moves:=checklistbox1.count-id; //最下行
     checklistbox1.items.move(id,id+moves);
     checklistbox1.itemindex:=id-1;
    end;
      

  2.   

    呵,最后错啦:
     checklistbox1.itemindex:=id+moves;
      

  3.   

    能给个完整点的吗.
    我已经恨不得搞2个按牛实现updown 的功能了.都写好了,就差不知道checklistbox选中是怎么判断的了.
    checklistbox.checked[i] 是判断是否钩选.
    我要的功能是,只要选中 一条,未钩选的移动.
      

  4.   

    我表达不清楚,我意思是, 直接改变checklistbox 里面的条目,点选变蓝的条目,不是钩着的.
      

  5.   

    checklistbox.checked[i] 是判断第i行是否选择中。
    你直接调用Moveby(-1),是代表将当前选择中的行上移一行
    如果用打勾的,你可以将进行判断:
    下面将第一个打勾的上多一行
     for i:=0 to checklistbox.count-1 do
      if checklistbox.checked[i] then
       begin
        checklistbox.selected[i]:=true; 
        moveby(-1);
        break;
       end;//总之,使用Move功能,将第几行,移动到第几行
      

  6.   

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      UpDown1.Max := CheckListBox1.Items.Count - 1;
    end;procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
    var
      I, J: Integer;
      Str: string;
      bl: Boolean;
    begin
      for I := 0 to CheckListBox1.Items.Count - 1 do
      begin
        if CheckListBox1.Selected[I] then
        begin
          if ((I =0) and (Button = btNext) )or ((I = CheckListBox1.Items.Count ) and (Button = btPrev)) then
          begin
            Break;                 
          end;
          J := I;
          if Button = btNext then
            Dec(J)
          else
            Inc(J);
          CheckListBox1.Selected[I] := False;
          bl := CheckListBox1.Checked[I];
          Str := CheckListBox1.Items[I]; 
          CheckListBox1.Items.Delete(I);
          CheckListBox1.Items.Insert(J, Str);
          CheckListBox1.Selected[J] := True;
          CheckListBox1.Checked[J] := True;
          Break;
        end;
      end;end;procedure TForm1.CheckListBox1Click(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 0 to CheckListBox1.Items.Count - 1 do
      begin
        if CheckListBox1.Selected[I] then
        begin
          UpDown1.Position := UpDown1.Max - I;
        end;
      end;
    end;