如何使用ListBox中的ListBox.Items.CopyTo()方法将Items的数据输出到数组?
主要想实现将一个ListBox中的内容Copy到另一个ListBox中!
有没有其他更好方法呢?

解决方案 »

  1.   

    可以将listbox中的数据一条条加到另一个listbox中,速度也不会很慢
      

  2.   

    for(int i=0;i<=this.lbWoolType.Items.Count-1;i++)
    {
    //this.lbChoosedWoolType.Items.Insert(i,this.lbWoolType.Items[i].ToString());
    MessageBox.Show(this.lbWoolType.Items[i].ToString());
    }lbWoolType是左边的ListBox控件
    lbChoosedWoolType是右边的ListBox控件
    如何才能将左边的内容一次过加到右边的控件里呢?
      

  3.   

    string [] strA = new string[5];//如果listBox1的item为5个,
                                   //则定义大小为5的字符串数组,或者为更大的数组
    listBox1.Items.CopyTo(strA,0); //Copy到strA数组中来
    foreach( string str in strA)   //对listBox2的items添加
    {
        if(str != null)
            listBox2.Items.Add(str);
    }//该代码,已经测试通过
      

  4.   

    //通过数组再转拷贝到listbox,如下(执行通过):
    ListBox lb = new ListBox ();
    object[] l = new string [this.listBox1.Items.Count ];
    this.listBox1 .Items.CopyTo (l,0);for(int i = 0;i<this.listBox1.Items.Count ;i++)
    {
    lb.Items.Add (l[i]);
    }
      

  5.   


    ListBox lb1,lb2;//假设已有lb1,lb2
    string[] arr = null ;lb1.Items.CopyTo(arr,0);
    lb1.Items.Clear();lb2.Items.AddRange(arr);