博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
winfrom如何在listview中添加控件
阅读量:5232 次
发布时间:2019-06-14

本文共 1507 字,大约阅读时间需要 5 分钟。

private Button btn = new Button();        private void Form1_Load(object sender, EventArgs e)        {            ListViewItem[] lvs = new ListViewItem[3];            lvs[0] = new ListViewItem(new string[] { "行1列1", "行1列2", "" });            lvs[1]=new ListViewItem(new string[] { "行2列1", "行2列2", "" });            lvs[2]=new ListViewItem(new string[] { "行3列1", "行3列2", "" });            this.listView1.Items.AddRange(lvs);            btn.Visible=false;            btn.Text = "...";            btn.Click += this.button_Click;            this.listView1.Controls.Add(btn);            this.btn.Size = new Size(this.listView1.Items[0].SubItems[2].Bounds.Width,                this.listView1.Items[0].SubItems[2].Bounds.Height);        }        private void listView1_SelectedIndexChanged(object sender, EventArgs e)        {            if (this.listView1.SelectedItems.Count > 0)            {                this.btn.Location = new Point(this.listView1.SelectedItems[0].SubItems[2].Bounds.Left,                    this.listView1.SelectedItems[0].SubItems[2].Bounds.Top);                this.btn.Visible = true;            }        }        private void button_Click(object sender, EventArgs e)        {            MessageBox.Show(this.listView1.SelectedItems[0].SubItems[0].Text);        }

在我们设置用listview的时候,经常用到要动态地添加控件,这里是个很好的解决办法

https://p-blog.csdn.net/images/p_blog_csdn_net/wangyu065/EntryImages/20091014/gd.jpg

代码转载自http://bbs.csdn.net/topics/320091790

 

转载于:https://www.cnblogs.com/CoderAyu/p/8490163.html

你可能感兴趣的文章