欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# listview添加combobox到单元格的实现代码

程序员文章站 2024-02-22 08:20:40
实现代码: using system; using system.collections.generic; using system.componentmod...

实现代码:

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;
    }
  }
}