c# listview

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace installfirstproject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //labeledit指标子项标签是否可以编辑
            listView1.LabelEdit = true;
            listView1.Items.Add("a");
            listView1.Items.Add("b");
        }
        //添加子项
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text =="")
            {
                MessageBox.Show("要添加到LISTVIEW的项为空");
            }
            else
            {
                listView1.Items.Add(textBox1.Text.Trim());
                //listview各个子项的显示方式,经测试仅为list;每个项左边才会完整显示checkbox复选框
               
                //经测试如果子项显示方式为list且子项左侧为复选框时,button2_click选中子项进行删除某子项不起作用,注解掉对比测试下
                //listView1.View = View.List;
                //listView1.CheckBoxes = true;
                try
                {
                    //判断子项个数
                    if (listView1.Items.Count < 2)
                    {
                        listView1.Items.Add("给力");
                    }
                    else
                    {
                        //selected指标是否选中某个子项
                        listView1.Items[2].Selected = true;
                    }
                }
                catch(Exception ec)
                {
                    MessageBox.Show(ec.Message);
                }
                textBox1.Clear();
               

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //删除前判断是否有子项,否则删除会报异常;在listview中,类似于资源管理器右窗口
            if (listView1.Items.Count == 0)
            {
                MessageBox.Show("不用删除,没有子项");
                return;
            }
            //else if (listView1.SelectedItems.Count == 0)
            //{
            //    MessageBox.Show("有子项,但当前没有选中子项");

            //}
            else
            {
                //选中了子项
               
                //经测试selecteditems.clear方法失效
               // listView1.SelectedItems.Clear();
               // MessageBox.Show("已成功删除子项"+listView1.SelectedItems[listView1.SelectedItems[0].Index]);
                //移除所有子项
                //listView1.Items.Clear();

                //下列方法是取消选择吧
                listView1.SelectedItems.Clear();

                //remove为删除指定子项,方法参数listviewitem
               // listView1.Items.Remove();
                listView1.Items.RemoveAt(0);
                //int index = listView1.SelectedItems.IndexOf(listView1.SelectedItems);
            }
        }
      
    }
}

请使用浏览器的分享功能分享到微信等