(四⼗三)c#Winform⾃定义控件-Listview-HZHControls 官⽹
前提
⼊⾏已经7,8年了,⼀直想做⼀套漂亮点的⾃定义控件,于是就有了本系列⽂章。
GitHub:
码云:
如果觉得写的还⾏,请点个 star ⽀持⼀下吧
欢迎前来交流探讨:企鹅568015492
⿇烦博客下⽅点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
⽬录
⽤处及效果
使⽤分页控件效果
不使⽤分页控件效果
准备⼯作
我们需要元素控件,需要列表控件,另外为了具有更好的扩展性,元素控件实现接⼝,⽅便进⾏扩展
我们⽤到了分页控件,如果你还不了解,请移步查看
我们这⾥的元素控件⽤到圆⾓,故继承基类控件UCControlBase,如果不了解,请移步查看开始
添加⼀个接⼝,⽤来约束元素控件
1public interface IListViewItem
2    {
3///<summary>
4///数据源
5///</summary>
6object DataSource { get; set; }
7///<summary>
8///选中项事件
9///</summary>
10event EventHandler SelectedItemEvent;
11///<summary>
12///选中处理,⼀般⽤以更改选中效果
13///</summary>
14///<param name="blnSelected">是否选中</param>
15void SetSelected(bool blnSelected);
16    }
添加⼀个元素控件,命名UCListViewItem,我们这⾥继承基类控件UCControlBase,实现接⼝IListViewItem  1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9
10namespace HZH_Controls.Controls
11 {
12    [ToolboxItem(false)]
13public partial class UCListViewItem : UCControlBase, IListViewItem
14    {
15private object m_dataSource;
16public object DataSource
17        {
18get
19            {
20return m_dataSource;
21            }
22set
23            {
24                m_dataSource = value;
25                lblTitle.Text = value.ToString();
26            }
27        }
28
29public event EventHandler SelectedItemEvent;
30public UCListViewItem()
31        {
32            InitializeComponent();
33            lblTitle.MouseDown += lblTitle_MouseDown;
34        }
35
36void lblTitle_MouseDown(object sender, MouseEventArgs e)
37        {
38if (SelectedItemEvent != null)
39            {
40                SelectedItemEvent(this, e);
41            }
42        }
43
44public void SetSelected(bool blnSelected)
45        {
46if (blnSelected)
47this.FillColor = Color.FromArgb(255, 247, 245);
48else
49this.FillColor = Color.White;
50this.Refresh();
51        }
52    }
53 }
1namespace HZH_Controls.Controls
2 {
3partial class UCListViewItem
4    {
5///<summary>
6///必需的设计器变量。
7///</summary>
8private System.ComponentModel.IContainer components = null;
9
10///<summary>
11///清理所有正在使⽤的资源。
12///</summary>
13///<param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14protected override void Dispose(bool disposing)
15        {
16if (disposing && (components != null))
17            {
18                components.Dispose();
19            }
20base.Dispose(disposing);
21        }
22
23#region组件设计器⽣成的代码
24
25///<summary>
26///设计器⽀持所需的⽅法 - 不要
27///使⽤代码编辑器修改此⽅法的内容。
28///</summary>
29private void InitializeComponent()
30        {
31this.lblTitle = new System.Windows.Forms.Label();
32this.SuspendLayout();
33//
34// lblTitle
35//
36this.lblTitle.Dock = System.Windows.Forms.DockStyle.Fill;
37this.lblTitle.Location = new System.Drawing.Point(0, 0);
38this.lblTitle.Name = "lblTitle";
39this.lblTitle.Size = new System.Drawing.Size(107, 96);
40this.lblTitle.TabIndex = 0;
41this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
42//
43// UCListViewItem
44//
45this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
46this.BackColor = System.Drawing.Color.Transparent;
47this.Controls.Add(this.lblTitle);
48this.FillColor = System.Drawing.Color.White;
49this.IsRadius = true;
50this.IsShowRect = true;
51this.Name = "UCListViewItem";
52this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
53this.Size = new System.Drawing.Size(107, 96);
54this.ResumeLayout(false);
55
56        }
57
58#endregion
59
60private System.Windows.Forms.Label lblTitle;
61    }
62 }excel listview控件
View Code
然后需要⼀个列表来显⽰元素控件
添加⼀个⽤户控件,命名UCListView
⼀些属性
1int m_intCellWidth = 130;//单元格宽度
2int m_intCellHeight = 120;//单元格⾼度
3
4private Type m_itemType = typeof(UCListViewItem);
5
6        [Description("单元格类型,如果⽆法满⾜您的需求,你可以⾃定义单元格控件,并实现接⼝IListViewItem"), Category("⾃定义")]
7public Type ItemType
8        {
9get { return m_itemType; }
10set
11            {
12if (!typeof(IListViewItem).IsAssignableFrom(value) || !value.IsSubclassOf(typeof(Control)))
13throw new Exception("单元格控件没有继承实现接⼝IListViewItem");
14                m_itemType = value;
15            }
16        }
17
18private UCPagerControlBase m_page = null;
19///<summary>
20///翻页控件
21///</summary>
22        [Description("翻页控件,如果UCPagerControl不满⾜你的需求,请⾃定义翻页控件并继承UCPagerControlBase"), Category("⾃定义")] 23public UCPagerControlBase Page
24        {
25get { return m_page; }
26set
27            {
28                m_page = value;
29if (value != null)
30                {
31if (!typeof(IPageControl).IsAssignableFrom(value.GetType()) || !value.GetType().IsSubclassOf(typeof(UCPagerControlBase))) 32throw new Exception("翻页控件没有继承UCPagerControlBase");
33this.panMain.AutoScroll = false;
34                    panPage.Visible = true;
35this.Controls.SetChildIndex(panMain, 0);
36                    m_page.ShowSourceChanged += m_page_ShowSourceChanged;
37                    m_page.Dock = DockStyle.Fill;
38this.panPage.Controls.Clear();
39this.panPage.Controls.Add(m_page);
40                    GetCellCount();
41this.DataSource = m_page.GetCurrentSource();
42                }
43else
44                {
45this.panMain.AutoScroll = true;
46                    m_page = null;
47                    panPage.Visible = false;
48                }
49            }
50        }
51
52
53
54private object m_dataSource = null;
55
56        [Description("数据源,如果使⽤翻页控件,请使⽤翻页控件的DataSource"), Category("⾃定义")]
57public object DataSource
58        {
59get { return m_dataSource; }
60set
61            {
62if (value == null)
63return;
64if (!typeof(IList).IsAssignableFrom(value.GetType()))
65                {
66throw new Exception("数据源不是有效的数据类型,列表");
67                }
68                m_dataSource = value;
69                ReloadSource();
70            }
71        }
72
73int m_intCellCount = 0;//单元格总数
74        [Description("单元格总数"), Category("⾃定义")]
75public int CellCount
76        {
77get { return m_intCellCount; }
78private set
79            {
80                m_intCellCount = value;
81if (value > 0 && m_page != null)
82                {
83                    m_page.PageSize = m_intCellCount;
84                    m_page.Reload();
85                }
86            }
87        }
88
89private List<object> m_selectedSource = new List<object>();
90
91        [Description("选中的数据"), Category("⾃定义")]
92public List<object> SelectedSource
93        {
94get { return m_selectedSource; }
95set
96            {
97                m_selectedSource = value;
98                ReloadSource();
99            }
100        }
101
102private bool m_isMultiple = true;
103
104        [Description("是否多选"), Category("⾃定义")]
105public bool IsMultiple
106        {
107get { return m_isMultiple; }
108set { m_isMultiple = value; }
109        }
110
111        [Description("选中项事件"), Category("⾃定义")]
112public event EventHandler SelectedItemEvent;
113public delegate void ReloadGridStyleEventHandle(int intCellCount);
114///<summary>
115///样式改变事件
116///</summary>
117        [Description("样式改变事件"), Category("⾃定义")]
118public event ReloadGridStyleEventHandle ReloadGridStyleEvent;
⼀下辅助函数
1#region重新加载数据源
2///<summary>
3///功能描述:重新加载数据源
4///作  者:HZH
5///创建⽇期:2019-06-27 16:47:32
6///任务编号:POS
7///</summary>
8public void ReloadSource()
9        {
10            ControlHelper.FreezeControl(this, true);
11if (this.panMain.Controls.Count <= 0)
12            {
13                ReloadGridStyle();
14            }
15if (m_dataSource == null || ((IList)m_dataSource).Count <= 0)
16            {
17for (int i = this.panMain.Controls.Count - 1; i >= 0; i--)
18                {
19this.panMain.Controls[i].Visible = false;
20                }
21
22return;
23            }
24int intCount = Math.Min(((IList)m_dataSource).Count, this.panMain.Controls.Count);