给你一个自己写的通用过程(listbox){************************************************************************** 功能描述:用于在两个ListBox中各种情况下互相移动数据 应用范围: 适应于ListBox中数据移走的所有情况 参数说明: IsAll 表示是否全部移动当前数据 SourceBox表示源ListBox,SourceList为保存有源隐含代码的自定义TStrings类型 TargetBox表示目标ListBox,TargetList为保存目标隐含代码的自定义TStrings类型 作者:Bruin ************************************************************************** } procedure TPublic.ListItemMove(IsAll: Boolean; SourceBox: TListBox; SourceList: TStringList; TargetBox: TListBox; TargetList: TStringList); var SelCount:Integer; //所有数据项移动的情况 procedure AllMove; var j:Integer; begin for j:=0 to SourceBox.Items.Count-1 do begin TargetBox.Items.Add(SourceBox.Items[j]); TargetList.Add(SourceList.Strings[j]); end; SourceBox.Items.Clear; SourceList.Clear; TargetBox.Selected[0]:=True; end; //多个数据项移动的情况 procedure MultiMove; var i,j:Integer; begin j:=0; for i:=SourceBox.Items.Count-1 downto 0 do if SourceBox.Selected[i] then begin j:=i; TargetBox.Items.Add(SourceBox.Items[i]); TargetList.Add(SourceList.Strings[i]); SourceBox.Items.Delete(i); SourceList.Delete(i); end; if j>SourceBox.Items.Count-1 then j:=SourceBox.Items.Count-1; SourceBox.Selected[j]:=True; end; //单个数据项移动的情况 Procedure SingleMove; var SelectIdx:Integer; begin SelectIdx:=SourceBox.ItemIndex; if SelectIdx<0 then Exit; TargetBox.Items.Add(SourceBox.Items[SelectIdx]); TargetList.Add(SourceList.Strings[SelectIdx]); SourceBox.Items.Delete(SelectIdx); SourceList.Delete(SelectIdx); if SelectIdx<=SourceBox.Items.Count-1 then SourceBox.Selected[SelectIdx]:=True else SourceBox.Selected[SourceBox.Items.Count-1]:=True; end;begin if SourceBox.Items.Count-1<0 then Exit; if IsAll then AllMove //调用子过程 else begin SelCount:=SourceBox.SelCount; if SelCount=1 then SingleMove //调用子过程 else if SelCount>1 then MultiMove; //调用子过程 end;end;
实例很简单阿
以ListBox来说
lstbx2.items.add(lstbx1.selectitem.text);
lstbx1.items.delete(lstbx1.SelectIndex);
可能里面的成员变量不对,但大概意思是这样,
需要注意的仅仅是先添加再删除,不要先删除在添加了
用两个ListBox,各表示两个不同的部门
然后声明两个数组,各纪录两个ListBox的成员,
在放一个按钮,叫"转移"操作事像这样
lstbx2.items.add(lstbx1.selectitem.text);
lstbx1.items.delete(lstbx1.SelectIndex);
这样能做到ListBox1中消失选定项,而ListBox2中添加选定项,
然后在正确处理两个数组就好了
功能描述:用于在两个ListBox中各种情况下互相移动数据
应用范围: 适应于ListBox中数据移走的所有情况
参数说明: IsAll 表示是否全部移动当前数据
SourceBox表示源ListBox,SourceList为保存有源隐含代码的自定义TStrings类型
TargetBox表示目标ListBox,TargetList为保存目标隐含代码的自定义TStrings类型
作者:Bruin
************************************************************************** }
procedure TPublic.ListItemMove(IsAll: Boolean; SourceBox: TListBox;
SourceList: TStringList; TargetBox: TListBox; TargetList: TStringList);
var
SelCount:Integer;
//所有数据项移动的情况
procedure AllMove;
var
j:Integer;
begin
for j:=0 to SourceBox.Items.Count-1 do
begin
TargetBox.Items.Add(SourceBox.Items[j]);
TargetList.Add(SourceList.Strings[j]);
end;
SourceBox.Items.Clear;
SourceList.Clear;
TargetBox.Selected[0]:=True;
end;
//多个数据项移动的情况
procedure MultiMove;
var
i,j:Integer;
begin
j:=0;
for i:=SourceBox.Items.Count-1 downto 0 do
if SourceBox.Selected[i] then
begin
j:=i;
TargetBox.Items.Add(SourceBox.Items[i]);
TargetList.Add(SourceList.Strings[i]);
SourceBox.Items.Delete(i);
SourceList.Delete(i);
end;
if j>SourceBox.Items.Count-1 then
j:=SourceBox.Items.Count-1;
SourceBox.Selected[j]:=True;
end;
//单个数据项移动的情况
Procedure SingleMove;
var
SelectIdx:Integer;
begin
SelectIdx:=SourceBox.ItemIndex;
if SelectIdx<0 then Exit;
TargetBox.Items.Add(SourceBox.Items[SelectIdx]);
TargetList.Add(SourceList.Strings[SelectIdx]);
SourceBox.Items.Delete(SelectIdx);
SourceList.Delete(SelectIdx);
if SelectIdx<=SourceBox.Items.Count-1 then
SourceBox.Selected[SelectIdx]:=True
else
SourceBox.Selected[SourceBox.Items.Count-1]:=True;
end;begin
if SourceBox.Items.Count-1<0 then Exit;
if IsAll then AllMove //调用子过程
else
begin
SelCount:=SourceBox.SelCount;
if SelCount=1 then SingleMove //调用子过程
else if SelCount>1 then MultiMove; //调用子过程
end;end;
部门放ComboBox