请教怎么用updown 来控制checklistbox 里面的条目上下移动
请问如何用updown 来控制checklistbox 里面的条目上下移动?
请问如何用updown 来控制checklistbox 里面的条目上下移动?
比如checklistbox里面有
1
2
3
4
5
6
想做到选择一个条目比如是4,然后点updown来控制4条目的上下移动.
变成
1
4
2
3
5
6
------解决方案--------------------
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;
------解决方案--------------------
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;
请问如何用updown 来控制checklistbox 里面的条目上下移动?
比如checklistbox里面有
1
2
3
4
5
6
想做到选择一个条目比如是4,然后点updown来控制4条目的上下移动.
变成
1
4
2
3
5
6
------解决方案--------------------
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;
------解决方案--------------------
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;