两个列表框:ListBox1,ListBox2移动ListBox1的一个或几个项目到ListBox2中,已经做出来了。移动ListBox1中所有的项目到ListBox2,要怎么写代码呢?

解决方案 »

  1.   


    procedure TFmain.Button1Click(Sender: TObject);
    begin
     {1单选的移到2}
     if ListBox1.Items.Count>0 then
     if ListBox1.ItemIndex>-1 then begin
       ListBox2.Items.Add(ListBox1.Items.Strings[ListBox1.ItemIndex]);
       ListBox1.Items.Delete(ListBox1.ItemIndex);
     end;
    end;procedure TFmain.Button3Click(Sender: TObject);
    begin
    {2单选的移到1}
      if ListBox2.Items.Count>0 then
     if ListBox2.ItemIndex>-1 then begin
       ListBox1.Items.Add(ListBox2.Items.Strings[ListBox2.ItemIndex]);
       ListBox2.Items.Delete(ListBox2.ItemIndex);
     end;
    end;procedure TFmain.Button4Click(Sender: TObject);
    var
    i:integer;
    begin
     {2全部移到1中去}
     if ListBox2.Items.Count>0 then begin
       for i :=0  to ListBox2.Items.Count-1 do
     if  ListBox1.Items.IndexOf(ListBox2.Items.Strings[i])<0 then
      ListBox1.Items.Add(ListBox2.Items.Strings[i]);
      ListBox2.Items.Clear;
     end;
    end;procedure TFmain.Button2Click(Sender: TObject);
    var
    i:integer;
    begin
     {1全部移到2中去}
     if ListBox1.Items.Count>0 then begin
       for i :=0  to ListBox1.Items.Count-1 do
     if  ListBox2.Items.IndexOf(ListBox1.Items.Strings[i])<0 then
      ListBox2.Items.Add(ListBox1.Items.Strings[i]);
      ListBox1.Items.Clear;
     end;
    end;
      

  2.   

      listbox2.Items.Text :=listbox1.Items.Text ;
      listbox1.Clear;
      

  3.   


    procedure TForm1.Button1Click(Sender: TObject);
    begin
      while ListBox1.Items.Count>0 do
      begin
        ListBox2.Items.Add(ListBox1.Items[0]);
        ListBox1.Items.Delete(0);
      end;
    end;