如何删除listbos中选中的多项item,给段代码,谢谢。

解决方案 »

  1.   

    for (int i = ListBox1.Items.Count - 1; i >= 0;i-- ) {
                ListItem item = ListBox1.Items[i];
                if (item.Selected) {
                    ListBox1.Items.Remove(item);
                }
            }
      

  2.   

    ListItem需要using哪个类? 
    编译的时候通不过
      

  3.   

    这样写肯定有问题-----------------------------------------------------------
    for (int i = ListBox1.Items.Count - 1; i >= 0;i-- ) {
                ListItem item = ListBox1.Items[i];
                if (item.Selected) {
                    ListBox1.Items.Remove(item);
                }
            }
    -----------------------------------------------------------Remove了你再进行循环肯定会崩溃的
    不过基本思想倒是还行,但是要换种方法
    不知道楼主是在web里还是Winform里、
      

  4.   

    ListItem是WEB里的东西。属于System.Web.UI.WebControls 命名空间下
      

  5.   

    Remove了你再进行循环肯定会崩溃的
    不过基本思想倒是还行,但是要换种方法
    不知道楼主是在web里还是Winform里、
    --------------------------------------------
    是在winform里。不过他的算法没有问题,是倒着删除的,所以不会出现删错的现象
      

  6.   

    WINFORM里就好弄了
    (不好意思刚才没注意上面是倒着删除的)
    获得选中结合集SelectedIndexCollection ,然后循环删除即可
    也是倒着删除的 
    ———————————————————————————————————— ListBox.SelectedIndexCollection ls = listBox1.SelectedIndices;            for (int i = ls.Count - 1; i >= 0; i--)
                {
                    listBox1.Items.RemoveAt(ls[i]);
                }————————————————————————————————————
      

  7.   

    using System.Collections;
            foreach (ListItem i in ListBox1.Items)
            {
                ListBox1.Items.Remove(i.Selected);
                if(ListBox1.SelectedItem==null)
    break;
            }
      

  8.   

    ListItemCollection ltc = new ListItemCollection();        foreach (ListItem lt in ListBox1.Items)
            {
                if (lt.Selected)
                    ltc.Add(lt);
            }
            foreach (ListItem lt2 in ltc)
                ListBox1.Items.Remove(lt2);