C# listview添加combobox到單位格的完成代碼。本站提示廣大學習愛好者:(C# listview添加combobox到單位格的完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C# listview添加combobox到單位格的完成代碼正文
完成代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { ListViewItem lvi; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { 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("鄧肯"); ListViewItem item; item = new ListViewItem(1.ToString()); item.SubItems.Add("姚明"); item.SubItems.Add("科比"); listView1.Items.Add(item); item = new ListViewItem(2.ToString()); item.SubItems.Add("鄧肯"); item.SubItems.Add("杜蘭特"); listView1.Items.Add(item); } private void listView1_MouseUp(object sender, MouseEventArgs e) { lvi = this.listView1.GetItemAt(e.X, e.Y); if (lvi != null) { //獲得選中行的Bounds Rectangle Rect = lvi.Bounds; int LX = listView1.Columns[0].Width; int RX = 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(); //} // int intColIndex = lvi.SubItems.IndexOf(lvi.GetSubItemAt(e.X, e.Y)); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { lvi.SubItems[1].Text = comboBox1.Text; // comboBox1.Visible = false; } private void comboBox1_MouseLeave(object sender, EventArgs e) { lvi.SubItems[1].Text = comboBox1.Text; // comboBox1.Visible = false; } } }