WinForm关于listview的用法介绍

public Form1()

        {

            InitializeComponent();

            //控件的行为

            listView1.Bounds = new Rectangle(new Point(10, 10), new Size(240, 200));//相对位置

            listView1.View = View.Details;//显示任意数量的列,只有第一列可以包含图标

            listView1.LabelEdit = true;

            listView1.GridLines = true;//显示网格

            listView1.Sorting = SortOrder.None;//对内容排序、按照第一列



            //增加列

            listView1.Columns.Add("姓名",-2,HorizontalAlignment.Left);//参数:列名、宽度、对齐方式

            listView1.Columns.Add("性别", -2, HorizontalAlignment.Left);

            listView1.Columns.Add("出生日期",-2,HorizontalAlignment.Left);

            listView1.Columns.Add("年龄",-2,HorizontalAlignment.Center);

            //创建item

            ListViewItem item1 = new ListViewItem();

            item1.Checked = true;

            item1.Text = "万爱珍";

            item1.SubItems.Add("");

            item1.SubItems.Add("1975");

            item1.SubItems.Add("32");

            ListViewItem item2 = new ListViewItem("刘平云", 1);

            item2.Checked = false;

            item2.SubItems.Add("");

            item2.SubItems.Add("1947");

            item2.SubItems.Add("60");

            listView1.Items.AddRange(new ListViewItem[]{item1,item2});

        }



        private void button1_Click(object sender, EventArgs e)

        {

            //动态添加listview项目

            ListViewItem item = new ListViewItem();

            item.Text = textBox1.Text;

            item.SubItems.Add(textBox2.Text);

            item.SubItems.Add(textBox3.Text);

            item.SubItems.Add(textBox4.Text);

            listView1.Items.AddRange(new ListViewItem[]{item});

            textBox1.Clear();//清除textbox1中的内容

            textBox2.Clear();

            textBox3.Clear();

            textBox4.Clear();

        }



        private void listView1_ItemActivate(object sender, EventArgs e)//选中listview项

        {

            ListView lw = (ListView)sender;

            textBox1.Text = lw.SelectedItems[0].SubItems[0].Text.ToString();

            textBox2.Text = lw.SelectedItems[0].SubItems[1].Text.ToString();

            textBox3.Text = lw.SelectedItems[0].SubItems[2].Text.ToString();

            textBox4.Text = lw.SelectedItems[0].SubItems[3].Text.ToString();

        }

private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                item.Remove();//删除方法
            }
        }

 WinForm关于listview的用法介绍

你可能感兴趣的:(ListView)