C# listview添加combobox到单元格的实现代码
实现代码:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; namespaceWindowsFormsApplication1 { publicpartialclassForm1:Form { ListViewItemlvi; publicForm1() { InitializeComponent(); } privatevoidForm1_Load(objectsender,EventArgse) { comboBox1.Visible=false; listView1.Columns.Add("第一列"); listView1.Columns.Add("第二列"); listView1.Columns.Add("第三列"); this.comboBox1.Items.Add("科比"); this.comboBox1.Items.Add("姚明"); this.comboBox1.Items.Add("杜兰特"); this.comboBox1.Items.Add("邓肯"); ListViewItemitem; item=newListViewItem(1.ToString()); item.SubItems.Add("姚明"); item.SubItems.Add("科比"); listView1.Items.Add(item); item=newListViewItem(2.ToString()); item.SubItems.Add("邓肯"); item.SubItems.Add("杜兰特"); listView1.Items.Add(item); } privatevoidlistView1_MouseUp(objectsender,MouseEventArgse) { lvi=this.listView1.GetItemAt(e.X,e.Y); if(lvi!=null) { //获取选中行的Bounds RectangleRect=lvi.Bounds; intLX=listView1.Columns[0].Width; intRX=listView1.Columns[0].Width+listView1.Columns[1].Width; //if(e.X>RX||e.X<LX) //{ this.comboBox1.Visible=false; Rect.X=listView1.Left+listView1.Columns[0].Width+2; Rect.Y=this.listView1.Top+2+Rect.Y; Rect.Width=listView1.Columns[1].Width+2; this.comboBox1.Bounds=Rect; this.comboBox1.Text=lvi.SubItems[1].Text; this.comboBox1.Visible=true; this.comboBox1.BringToFront(); this.comboBox1.Focus(); //} //intintColIndex=lvi.SubItems.IndexOf(lvi.GetSubItemAt(e.X,e.Y)); } } privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse) { lvi.SubItems[1].Text=comboBox1.Text; //comboBox1.Visible=false; } privatevoidcomboBox1_MouseLeave(objectsender,EventArgse) { lvi.SubItems[1].Text=comboBox1.Text; //comboBox1.Visible=false; } } }