merlinzjl

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

Winform ListView中使用combox填充单元格

 

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 WindowsFormsAppTest
{
    public partial class ListViewxc : ListView
    {
        private List<Control> controlsxc = new List<Control>();
        private const int WM_PAINT = 0x000F;
        public ListViewxc()
        {
            InitializeComponent();
 
            controlsxc.Clear();
 
            this.OwnerDraw = true;
            this.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(this_DrawColumnHeader);
            this.DrawSubItem += new DrawListViewSubItemEventHandler(this_DrawSubItem); 
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            foreach (Control item in controlsxc)
            {
                ListViewSubItemxc subItemxc = item.Tag as ListViewSubItemxc;
                if (subItemxc != null)
                {
                    Rectangle r = subItemxc.Bounds;
                    ComboBox combox = item as ComboBox;
                    if (combox != null)
                    {
                        if (r.Y > 0 && r.Y < this.ClientRectangle.Height)
                        {
                            combox.Visible = true;
                            combox.Bounds = new Rectangle(r.X + 2, r.Y + 2, r.Width - (2 * 2), r.Height - (2 * 2));
                            System.Diagnostics.Debug.WriteLine(",,," + r.X + " " + r.Y + " " + r.Width + " " + r.Height);
                        }
                        else
                        {
                            combox.Visible = false;
                        }
                    }
                }
            }
 
            base.OnPaint(e);
        }
 
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_PAINT)
            {
                foreach (Control item in controlsxc)
                {
                    ListViewSubItemxc subItemxc = item.Tag as ListViewSubItemxc;
                    if (subItemxc != null)
                    {
                        Rectangle r = subItemxc.Bounds;
                        ComboBox combox = item as ComboBox;
                        if (combox != null)
                        {
                            if (r.Y > 0 && r.Y < this.ClientRectangle.Height)
                            {
                                combox.Visible = true;
                                combox.Bounds = new Rectangle(r.X + 2, r.Y + 2, r.Width - (2 * 2), r.Height - (2 * 2));
                                System.Diagnostics.Debug.WriteLine(",,," + r.X + " " + r.Y + " " + r.Width + " " + r.Height);
                            }
                            else
                            {
                                combox.Visible = false;
                            }
                        }
                    }
                }
            }
 
            base.WndProc(ref m);
        }
 
        private void this_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            e.DrawDefault = true;
        }
 
        private void this_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }
 
        public void AddItemxc(ListViewItem item)
        {
            foreach (ListViewItem.ListViewSubItem it in item.SubItems)
            {
                ListViewSubItemxc x = it as ListViewSubItemxc;
                if (x != null && x._control != null)
                {
                    this.Controls.Add(x._control);
                    x._control.Tag = x;
 
                    ComboBox co = x._control as ComboBox;
                    if (co != null)
                    {
                        co.SelectedIndexChanged += Co_SelectedIndexChanged;
                        controlsxc.Add(co);
                    }
                }
            }
 
            this.Items.Add(item);
        }
 
        private void Co_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox co = sender as ComboBox;
            ListViewSubItemxc x = co.Tag as ListViewSubItemxc;
            if (x != null)
            {
                x.Text = co.SelectedText;
            }
        }
    }
 
    public class ListViewSubItemxc : ListViewItem.ListViewSubItem
    {
        public Control _control = null;
 
        public ListViewSubItemxc(Control con)
        {
            _control = con;
        }
    }
}

  

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
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 WindowsFormsAppTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
            ListViewItem l = new ListViewItem();
            l.Text = "a";
            ComboBox cb = new ComboBox();
            cb.DropDownStyle = ComboBoxStyle.DropDownList;
            cb.Items.Add("yellow");
            cb.Items.Add("red");
            cb.SelectedIndex = 0;
            ListViewSubItemxc lxc = new ListViewSubItemxc(cb);
 
            ListViewItem l2 = new ListViewItem();
            ComboBox cb2 = new ComboBox();
            cb2.DropDownStyle = ComboBoxStyle.DropDownList;
            cb2.Items.Add("yellow2");
            cb2.Items.Add("red2");
            cb2.SelectedIndex = 1;
            ListViewSubItemxc lxc2 = new ListViewSubItemxc(cb2);
 
            l.SubItems.Add(lxc);
            l2.SubItems.Add(lxc2);
            listViewxc1.AddItemxc(l);
            listViewxc1.AddItemxc(l2);
        }
    }
}

  

 

posted on   merlinzjl  阅读(254)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-06-02 Qt Creator快捷键设置
点击右上角即可分享
微信分享提示