zoukankan      html  css  js  c++  java
  • ListBox控件怎样删除选择的多个Item(c#,经典)

    ListBox控件默认情况下只可以进行单选,如要进行多选操作,需要将SelectionMode属性设置成SelectionMode.MultiSimple。

        如果需要将ListBox选择的多项Item删除,或要进行其它的操作该怎样捕获这些信息呢?

            private void button_deleteSelected_Click(object sender, EventArgs e)

             {

                  ListBox.SelectedIndexCollection sic = listBox_demo.SelectedIndices;//得到选择的Item的下标

                  if (sic.Count == 0)

                       return;

                  else

                  {

                       //  将选择的Item放入list中

                       List<int> list = new List<int>();

                       for (int i = 0; i < sic.Count; i++)

                       {

                           list.Add(sic[i]);

                       }

                       list.Sort();//对list进行排序(库里默认的排序结果一般指的是从下到大的排序)

                       while(list.Count != 0)//按照下标从大到小的顺序从ListBox控件里删除选择的Item

                       //如果这里采用其它顺序则可能破坏下标的有效性

                       {

                           listBox_demo.Items.RemoveAt(list[list.Count - 1]);

                           list.RemoveAt(list.Count - 1);

                       }

                  }

             }

            有一种方法更简单:

                while (listBox1.SelectedItems.Count != 0)
                {

                    listBox1.Items.RemoveAt(listBox1.SelectedIndices[0]);
                }

                while (listBox1.SelectedItems.Count != 0)
                {

                    listBox1.Items.RemoveAt(listBox1.SelectedIndices[0]);
                }


     

  • 相关阅读:
    安卓中期小作业
    安卓大作业UI预定搞
    实验3
    实验一总结
    实验8 SQLite数据库操作
    实验6 在应用程序中播放音频和视频
    实验4 颜色、字符串资源的使用
    实验四
    实验三
    实验二
  • 原文地址:https://www.cnblogs.com/chenbg2001/p/1373573.html
Copyright © 2011-2022 走看看