如下就行了:
private void Button1_Click(object sender, System.EventArgs e)
{
     foreach(int index in ListBox1.SelectedIndices)
     {
          ListBox2.Items.Add(ListBox1.Items[index].Text);
          ListBox1.Items.RemoveAt(index);
     }
}

解决方案 »

  1.   

    ListBox1.Items[ListBox1.Items.Count-1].Selected=false;
    这句话是干什么的??还有你应该从后面往前处理~~ 也就是说从n~ 0~~
    不然处理到后面会超出边界~
      

  2.   

    Knight94(愚翁)
    错了~~ 你在foreach里面改变了枚举的元素~
      

  3.   

    你的程序,一边添加一边删除,小心越界。还有你的这句
    ListBox1.Items[ListBox1.Items.Count-1].Selected=false;
    要做什么?
      

  4.   

    private void btn_toRight_Click(object sender, System.EventArgs e)
    {
    //如果没选中左边listBox中的项则出现对话框。
    if(listBox1.SelectedIndex==-1)
    {
    MessageBox.Show("没选中");
    }
    else
    {
    //右边的listBox中加入左边listBox的项
    listBox2.Items.Add(listBox1.Items[listBox1.SelectedIndex]);
    //把左边的项给去掉。
    listBox1.Items.Remove(listBox1.Items[listBox1.SelectedIndex]);
    }
    }
      

  5.   

    谢谢 cherno(且歌且狂) ,我犯了一个错误,下面这样就行了:
    foreach(int index in listBox1.SelectedIndices)
    listBox2.Items.Add(listBox1.Items[index].ToString());for(int i=listBox1.SelectedIndices.Count-1;i>=0;i--)
    {
    listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
    }
      

  6.   

    不好意思,我没说清楚
    我的listbox1中的内容是通过数据库中的一个字段绑定的
    ListBox1.DataTextField="name";
    ListBox1.DataValueField="name";
    我写的if (listbox1.selectedindex!=-1) {
    .....
    }
    明明我选择了其中的一项,但却就是=-1 ,各位大侠,请问为什么,谢谢了?
      

  7.   

    private void listBox1_DoubleClick(object sender, System.EventArgs e)
    {listBox2.Items.add(listBox1.Text);
     listBox1.Items.Remove(listBox1.SelectedItem);
    }
      
    }
      

  8.   

    上面的大侠,你那是不对的
    请再看我的问题
    不好意思,我没说清楚
    我的listbox1中的内容是通过数据库中的一个字段绑定的
    ListBox1.DataTextField="name";
    ListBox1.DataValueField="name";
    我写的if (listbox1.selectedindex!=-1) {
    ListBox2.Items.Add(new ListItem(ListBox1.SelectedItem.Value));
    ListBox1.Items.Remove(ListBox1.SelectedItem.Value);
    }
    明明我选择了其中的一项,但却就是=-1 ,各位大侠,请问为什么,谢谢了?
      

  9.   

    换成listBox1.selectedItems[0].index试试,我怎么找不到你listbox1.selectedindex捏?