亲宝软件园·资讯

展开

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

人气:0
想了解C# listview添加combobox到单元格的实现代码的相关内容吗,在本文为您仔细讲解listview添加combobox到单元格的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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;
    }
  }
}

加载全部内容

相关教程
猜你喜欢
用户评论