如题,放一个按钮,使得CheckListBox里面选择的Item能够向上或者向下移动?

解决方案 »

  1.   

    两个botton事件,搞定了,很简单的,多看看vcl控件使用的资料
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      //上移操作
      if CheckListBox1.ItemIndex > 0 then
        CheckListBox1.Items.Move(CheckListBox1.ItemIndex, CheckListBox1.ItemIndex - 1);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      //下移操作
      if CheckListBox1.ItemIndex = -1 then Exit;
      if CheckListBox1.ItemIndex < CheckListBox1.Items.Count - 1 then
      CheckListBox1.Items.Move(CheckListBox1.ItemIndex, CheckListBox1.ItemIndex + 1);end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var s:string;
        ck1,ck2:boolean;
        i:integer;
    begin
      i:=checklistbox1.ItemIndex;
      if i<CheckListBox1.Count-1 then
      begin
        ck1:=CheckListBox1.Checked[i];
        ck2:=CheckListBox1.Checked[i+1];
        s:=CheckListBox1.Items[i];
        CheckListBox1.Items[i]:=CheckListBox1.Items[i+1];
        CheckListBox1.Items[i+1]:=s;
        CheckListBox1.Checked[i]:=ck2;
        CheckListBox1.Checked[i+1]:=ck1;
        CheckListBox1.ItemIndex:=i+1;
      end;
    end;
      

  3.   

    这样会更好的procedure TForm1.Button1Click(Sender: TObject);
    var
      iCurrentIndex: Integer;
    begin
      //上移操作  if CheckListBox1.ItemIndex > 0 then
      begin
        iCurrentIndex := CheckListBox1.ItemIndex - 1;
        CheckListBox1.Items.Move(CheckListBox1.ItemIndex, CheckListBox1.ItemIndex - 1);
      end;  CheckListBox1.SetFocus;
      CheckListBox1.ItemIndex := iCurrentIndex;
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      iCurrentIndex: Integer;
    begin
      //下移操作
      if CheckListBox1.ItemIndex = -1 then Exit;
      if CheckListBox1.ItemIndex < CheckListBox1.Items.Count - 1 then
      begin
        iCurrentIndex :=  CheckListBox1.ItemIndex + 1;
        CheckListBox1.Items.Move(CheckListBox1.ItemIndex, iCurrentIndex);
      end;
      CheckListBox1.SetFocus;
      CheckListBox1.ItemIndex := iCurrentIndex;
    end;