VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C# listview添加combobox到单元格的实现代码

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;
    }
  }
}


相关教程