标签:
因为要在项目中要在ListView中实现下拉框选择,用DataGrid的话,一个不美观,二个绑定数据麻烦,参考网上一种做法,就是单击ListView时,判断单击的区域,然后将Combox控件显示单击的区域,以模拟效果,很少写winform,写的不好,望大家不要笑话。
先在容器中拖入一个ListView控件和Combox控件,然后设置Combox控件的Visible属性为False,即隐藏Combox控件,如图:
随便填充点数据到ListView和Combox中,如下:
1 this.listView1.SmallImageList = imageList1; 2 this.comboBox2.Items.Add("苹果"); 3 this.comboBox2.Items.Add("香蕉"); 4 this.comboBox2.Items.Add("橘子"); 5 this.comboBox2.Items.Add("葡萄"); 6 ListViewItem item; 7 item = new ListViewItem(1.ToString()); 8 item.SubItems.Add("苹果"); 9 item.SubItems.Add("香蕉"); 10 listView1.Items.Add(item); 11 item = new ListViewItem(2.ToString()); 12 item.SubItems.Add("橘子"); 13 item.SubItems.Add("葡萄"); 14 listView1.Items.Add(item);
该类主要实现点击后将Combox控件显示到点击的区域中,同时,将Combox的SelectedIndexChanged和Leave事件也实现在该类中,代码如下:
1 public class ListViewCombox 2 { 3 ListView _listView; 4 ComboBox _combox; 5 int _showColumn = 0; 6 ListViewSubItem _selectedSubItem; 7 8 /// <summary> 9 /// 列表combox 10 /// </summary> 11 /// <param name="listView">listView控件</param> 12 /// <param name="combox">要呈现的combox控件</param> 13 /// <param name="showColumn">要在哪一列显示combox(从0开始)</param> 14 public ListViewCombox(ListView listView, ComboBox combox, int showColumn) { 15 _listView = listView; 16 _combox = combox; 17 _showColumn = showColumn; 18 BindComboxEvent(); 19 } 20 21 /// <summary> 22 /// 定位combox 23 /// </summary> 24 /// <param name="x">点击的x坐标</param> 25 /// <param name="y">点击的y坐标</param> 26 public void Location(int x, int y) { 27 ListViewItem item = _listView.GetItemAt(x, y); 28 if (item != null) { 29 _selectedSubItem = item.GetSubItemAt(x, y); 30 if (_selectedSubItem != null) { 31 int clickColumn = item.SubItems.IndexOf(_selectedSubItem); 32 if (clickColumn == 0) { 33 _combox.Visible = false; 34 } 35 else if (clickColumn == _showColumn) { 36 int padding = 2; 37 Rectangle rect = _selectedSubItem.Bounds; 38 rect.X += _listView.Left + padding; 39 rect.Y += _listView.Top + padding; 40 rect.Width = _listView.Columns[clickColumn].Width + padding; 41 if (_combox != null) { 42 _combox.Bounds = rect; 43 _combox.Text = _selectedSubItem.Text; 44 _combox.Visible = true; 45 _combox.BringToFront(); 46 _combox.Focus(); 47 } 48 } 49 } 50 } 51 } 52 53 private void BindComboxEvent() { 54 if (_combox != null) { 55 _combox.SelectedIndexChanged += combox_SelectedIndexChanged; 56 _combox.Leave += combox_Leave; 57 } 58 } 59 60 private void combox_Leave(object sender, EventArgs e) { 61 if (_selectedSubItem != null) { 62 _selectedSubItem.Text = _combox.Text; 63 _combox.Visible = false; 64 } 65 } 66 67 private void combox_SelectedIndexChanged(object sender, EventArgs e) { 68 if (_selectedSubItem != null) { 69 _selectedSubItem.Text = _combox.Text; 70 _combox.Visible = false; 71 } 72 } 73 }
在容器代码中,先声明一个ListViewCombox类的全局实例,并在构造函数中实例化,最后在ListView中MouseUp事件中实现定位,如图:
因为上一步实例化ListViewCombox类时我传入的显示列为1,所以在ListView控件中单击第二列时就会产生下拉效果,如图:
很少写winform,但我感觉代码都是相通的,最主要的是理解你要实现什么,然后进行抽象和封装,希望对大家能有所帮助,也请高手对我进行指导,谢谢!
标签:
原文地址:http://www.cnblogs.com/yezhu008/p/5799567.html