题目如下:
   创建一个包含两个列表框的Form。在设计时使用“属性”窗口添加一些素到一个列表框中。创建一个按钮,当单击这个按钮时,将第一个列表中的选中元素删除(大于等于一个元素。),并将该无添加到第二个列表中。列表框的元素为
yang
ye
mao
xiao
wu列表框1 SelectionMode 属性为 MultiExtended。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ListsCopy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void btnCopy_Click(object sender, EventArgs e)
        {
            for (int intIndex = 0; intIndex < lstBox1.SelectedItems.Count; intIndex++)
            {
                lstBox1.Items.RemoveAt(intIndex);
                lstBox2.Items.Add(lstBox1.SelectedItems[intIndex]);
            }
        }        private void lstBox2_SelectedIndexChanged(object sender, EventArgs e)
        {        }
    }
}试了好多方法的
我这个代码还是有问题。能把选中的添加到列表框2,但是不能把选中的从列表1删除。删除的而是第一和第三个元素。
而且还有数组越界的问题。 
附件传不上来!!!!!
达人们把我修改一下。小生在这里谢谢你们了

解决方案 »

  1.   

    lstBox1.Items.RemoveAt(lstBox1.selectedIndex);
    lstBox2.Items.Add(lstBox1.SelectedItems.text);
    不用循环如果没记错就是这样
      

  2.   

    你说的功能实现不需要For循环来做,将语句改写如下:
          if(lstBox1.SelectedIndex != -1)
                {
                    lstBox2.Items.Add(lstBox1.SelectedItem.ToString());
                    lstBox1.Items.Remove(lstBox1.SelectedItem);
                }
    如果想将lstBox1中的全部元素转到lstBox2中的话,用以下语句:
                for (int i = 0; i < lstBox1.Items.Count; i++)
                {
                    lstBox1.SelectedIndex = i;
                    lstBox2.Items.Add(lstBox1.SelectedItem.ToString());
                }
                lstBox1.Items.Clear();
      

  3.   

    对了,在ListBox中选择项时,一次只能选一个,不能多选,所以你的For语句无意义。
      

  4.   


    有错误。  lstBox1.SelectedItems.Text   没有Text这个方法。
      

  5.   

    那就是value这个属性了或者楼上说的直接selectedItem.ToString()