前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
麻煩博客下方點個【推薦】,謝謝
NuGet
Install-Package HZH_Controls
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
用處及效果
1 List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>(); 2 lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "編號", Width = 70, WidthType = SizeType.Absolute }); 3 lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = 100, WidthType = SizeType.Absolute }); 4 lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年齡", Width = 100, WidthType = SizeType.Absolute }); 5 lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = 120, WidthType = SizeType.Absolute, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } }); 6 lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sex", HeadText = "性別", Width = 100, WidthType = SizeType.Absolute, Format = (a) => { return ((int)a) == 0 ? "女" : "男"; } }); 7 this.ucComboxGrid1.GridColumns = lstCulumns; 8 List<object> lstSourceGrid = new List<object>(); 9 for (int i = 0; i < 100; i++) 10 { 11 TestModel model = new TestModel() 12 { 13 ID = i.ToString(), 14 Age = 3 * i, 15 Name = "姓名——" + i, 16 Birthday = DateTime.Now.AddYears(-10), 17 Sex = i % 2 18 }; 19 lstSourceGrid.Add(model); 20 } 21 this.ucComboxGrid1.GridDataSource = lstSourceGrid;
準備工作
此控制項繼承自UCCombox,並且需要表格控制項UCDataGridView,如果不瞭解請移步查看
如果你想顯示樹形結構,請移步查看
(四十七)c#Winform自定義控制項-樹表格(treeGrid)
開始
我們首先需要一個彈出的面板,那麼我們先添加一個用戶控制項UCComboxGridPanel,在這個用戶控制項上添加一個文本框進行搜索,添加一個表格展示數據
一些屬性
1 [Description("項點擊事件"), Category("自定義")] 2 public event DataGridViewEventHandler ItemClick; 3 private Type m_rowType = typeof(UCDataGridViewRow); 4 5 public Type RowType 6 { 7 get { return m_rowType; } 8 set 9 { 10 m_rowType = value; 11 this.ucDataGridView1.RowType = m_rowType; 12 } 13 } 14 15 private List<DataGridViewColumnEntity> m_columns = null; 16 17 public List<DataGridViewColumnEntity> Columns 18 { 19 get { return m_columns; } 20 set 21 { 22 m_columns = value; 23 this.ucDataGridView1.Columns = value; 24 } 25 } 26 private List<object> m_dataSource = null; 27 28 public List<object> DataSource 29 { 30 get { return m_dataSource; } 31 set { m_dataSource = value; } 32 } 33 34 private string strLastSearchText = string.Empty; 35 UCPagerControl m_page = new UCPagerControl();
一些事件,處理數據綁定
1 void ucDataGridView1_ItemClick(object sender, DataGridViewEventArgs e) 2 { 3 if (ItemClick != null) 4 { 5 ItemClick((sender as IDataGridViewRow).DataSource, null); 6 } 7 } 8 9 void txtInput_TextChanged(object sender, EventArgs e) 10 { 11 timer1.Enabled = false; 12 timer1.Enabled = true; 13 } 14 15 private void UCComboxGridPanel_Load(object sender, EventArgs e) 16 { 17 m_page.DataSource = m_dataSource; 18 this.ucDataGridView1.DataSource = m_page.GetCurrentSource(); 19 } 20 21 private void timer1_Tick(object sender, EventArgs e) 22 { 23 if (strLastSearchText == txtSearch.InputText) 24 { 25 timer1.Enabled = false; 26 } 27 else 28 { 29 strLastSearchText = txtSearch.InputText; 30 Search(txtSearch.InputText); 31 } 32 } 33 34 private void Search(string strText) 35 { 36 m_page.StartIndex = 0; 37 if (!string.IsNullOrEmpty(strText)) 38 { 39 strText = strText.ToLower(); 40 List<object> lst = m_dataSource.FindAll(p => m_columns.Any(c => (c.Format == null ? (p.GetType().GetProperty(c.DataField).GetValue(p, null).ToStringExt()) : c.Format(p.GetType().GetProperty(c.DataField).GetValue(p, null))).ToLower().Contains(strText))); 41 m_page.DataSource = lst; 42 } 43 else 44 { 45 m_page.DataSource = m_dataSource; 46 } 47 m_page.Reload(); 48 }
完整代碼
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace HZH_Controls.Controls.ComboBox 11 { 12 [ToolboxItem(false)] 13 public partial class UCComboxGridPanel : UserControl 14 { 15 [Description("項點擊事件"), Category("自定義")] 16 public event DataGridViewEventHandler ItemClick; 17 private Type m_rowType = typeof(UCDataGridViewRow); 18 19 public Type RowType 20 { 21 get { return m_rowType; } 22 set 23 { 24 m_rowType = value; 25 this.ucDataGridView1.RowType = m_rowType; 26 } 27 } 28 29 private List<DataGridViewColumnEntity> m_columns = null; 30 31 public List<DataGridViewColumnEntity> Columns 32 { 33 get { return m_columns; } 34 set 35 { 36 m_columns = value; 37 this.ucDataGridView1.Columns = value; 38 } 39 } 40 private List<object> m_dataSource = null; 41 42 public List<object> DataSource 43 { 44 get { return m_dataSource; } 45 set { m_dataSource = value; } 46 } 47 48 private string strLastSearchText = string.Empty; 49 UCPagerControl m_page = new UCPagerControl(); 50 51 public UCComboxGridPanel() 52 { 53 InitializeComponent(); 54 this.ucDataGridView1.Page = m_page; 55 this.ucDataGridView1.IsAutoHeight = false; 56 this.txtSearch.txtInput.TextChanged += txtInput_TextChanged; 57 this.ucDataGridView1.ItemClick += ucDataGridView1_ItemClick; 58 } 59 60 void ucDataGridView1_ItemClick(object sender, DataGridViewEventArgs e) 61 { 62 if (ItemClick != null) 63 { 64 ItemClick((sender as IDataGridViewRow).DataSource, null); 65 } 66 } 67 68 void txtInput_TextChanged(object sender, EventArgs e) 69 { 70 timer1.Enabled = false; 71 timer1.Enabled = true; 72 } 73 74 private void UCComboxGridPanel_Load(object sender, EventArgs e) 75 { 76 m_page.DataSource = m_dataSource; 77 this.ucDataGridView1.DataSource = m_page.GetCurrentSource(); 78 } 79 80 private void timer1_Tick(object sender, EventArgs e) 81 { 82 if (strLastSearchText == txtSearch.InputText) 83 { 84 timer1.Enabled = false; 85 } 86 else 87 { 88 strLastSearchText = txtSearch.InputText; 89 Search(txtSearch.InputText); 90 } 91 } 92 93 private void Search(string strText) 94 { 95 m_page.StartIndex = 0; 96 if (!string.IsNullOrEmpty(strText)) 97 { 98 strText = strText.ToLower(); 99 List<object> lst = m_dataSource.FindAll(p => m_columns.Any(c => (c.Format == null ? (p.GetType().GetProperty(c.DataField).GetValue(p, null).ToStringExt()) : c.Format(p.GetType().GetProperty(c.DataField).GetValue(p, null))).ToLower().Contains(strText))); 100 m_page.DataSource = lst; 101 } 102 else 103 { 104 m_page.DataSource = m_dataSource; 105 } 106 m_page.Reload(); 107 } 108 } 109 }View Code
1 namespace HZH_Controls.Controls.ComboBox 2 { 3 partial class UCComboxGridPanel 4 { 5 /// <summary> 6 /// 必需的設計器變數。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的資源。 12 /// </summary> 13 /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region 組件設計器生成的代碼 24 25 /// <summary> 26 /// 設計器支持所需的方法 - 不要 27 /// 使用代碼編輯器修改此方法的內容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.components = new System.ComponentModel.Container(); 32 this.panel1 = new System.Windows.Forms.Panel(); 33 this.panel2 = new System.Windows.Forms.Panel(); 34 this.timer1 = new System.Windows.Forms.Timer(this.components); 35 this.ucControlBase1 = new HZH_Controls.Controls.UCControlBase(); 36 this.ucDataGridView1 = new HZH_Controls.Controls.UCDataGridView(); 37 this.txtSearch = new HZH_Controls.Controls.UCTextBoxEx(); 38 this.ucSplitLine_V2 = new HZH_Controls.Controls.UCSplitLine_V(); 39 this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V(); 40 this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H(); 41 this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H(); 42 this.panel1.SuspendLayout(); 43 this.ucControlBase1.SuspendLayout(); 44 this.SuspendLayout(); 45 // 46 // panel1 47 // 48 this.panel1.Controls.Add(this.ucControlBase1); 49 this.panel1.Controls.Add(this.panel2); 50 this.panel1.Controls.Add(this.txtSearch); 51 this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; 52 this.panel1.Location = new System.Drawing.Point(1, 1); 53 this.panel1.Name = "panel1"; 54 this.panel1.Padding = new System.Windows.Forms.Padding(5); 55 this.panel1.Size = new System.Drawing.Size(447, 333); 56 this.panel1.TabIndex = 4; 57 // 58 // panel2 59 // 60 this.panel2.Dock = System.Windows.Forms.DockStyle.Top; 61 this.panel2.Location = new System.Drawing.Point(5, 47); 62 this.panel2.Name = "panel2"; 63 this.panel2.Size = new System.Drawing.Size(437, 15); 64 this.panel2.TabIndex = 1; 65 // 66 // timer1 67 // 68 this.timer1.Interval = 1000; 69 this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 70 // 71 // ucControlBase1 72 // 73 this.ucControlBase1.ConerRadius = 5; 74 this.ucControlBase1.Controls.Add(this.ucDataGridView1); 75 this.ucControlBase1.Dock = System.Windows.Forms.DockStyle.Fill; 76 this.ucControlBase1.FillColor = System.Drawing.Color.Transparent; 77 this.ucControlBase1.Font = new System.Drawing.Font("微軟雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 78 this.ucControlBase1.IsRadius = false; 79 this.ucControlBase1.IsShowRect = true; 80 this.ucControlBase1.Location = new System.Drawing.Point(5, 62); 81 this.ucControlBase1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 82 this.ucControlBase1.Name = "ucControlBase1"; 83 this.ucControlBase1.Padding = new System.Windows.Forms.Padding(5); 84 this.ucControlBase1.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(220)))), ((int)(((byte)(220))))); 85 this.ucControlBase1.RectWidth = 1; 86 this.ucControlBase1.Size = new System.Drawing.Size(437, 266); 87 this.ucControlBase1.TabIndex = 2; 88 this.ucControlBase1.TabStop = false; 89 // 90 // ucDataGridView1 91 // 92 this.ucDataGridView1.BackColor = System.Drawing.Color.White; 93 this.ucDataGridView1.Columns = null; 94 this.ucDataGridView1.DataSource = null; 95 this.ucDataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; 96 this.ucDataGridView1.HeadFont = new System.Drawing.Font("微軟雅黑", 12F); 97 this.ucDataGridView1.HeadHeight = 40; 98 this.ucDataGridView1.HeadPadingLeft = 0; 99 this.ucDataGridView1.HeadTextColor = System.Drawing.Color.Black; 100 this.ucDataGridView1.IsAutoHeight = false; 101 this.ucDataGridView1.IsShowCheckBox = false; 102 this.ucDataGridView1.IsShowHead = true; 103 this.ucDataGridView1.Location = new System.Drawing.Point(5, 5); 104 this.ucDataGridView1.Name = "ucDataGridView1"; 105 this.ucDataGridView1.Page = null; 106 this.ucDataGridView1.RowHeight = 30; 107 this.ucDataGridView1.RowType = typeof(HZH_Controls.Controls.UCDataGridViewRow); 108 this.ucDataGridView1.Size = new System.Drawing.Size(427, 256); 109 this.ucDataGridView1.TabIndex = 0; 110 this.ucDataGridView1.TabStop = false; 111 // 112 // txtSearch 113 // 114 this.txtSearch.BackColor = System.Drawing.Color.Transparent; 115 this.txtSearch.ConerRadius = 5; 116 this.txtSearch.Cursor = System.Windows.Forms.Cursors.IBeam; 117 this.txtSearch.DecLength = 2; 118 this.txtSearch.Dock = System.Windows.Forms.DockStyle.Top; 119 this.txtSearch.FillColor = System.Drawing.Color.Empty; 120 this.txtSearch.Font = new System.Drawing.Font("微軟雅黑", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 121 this.txtSearch.InputText = ""; 122 this.txtSearch.InputType = HZH_Controls.TextInputType.NotControl; 123 this.txtSearch.IsFocusColor = true; 124 this.txtSearch.IsRadius = true; 125 this.txtSearch.IsShowClearBtn = true; 126 this.txtSearch.IsShowKeyboard = false; 127 this.txtSearch.IsShowRect = true; 128 this.txtSearch.IsShowSearchBtn = false; 129 this.txtSearch.KeyBoardType = HZH_Controls.Controls.KeyBoardType.UCKeyBorderAll_EN; 130 this.txtSearch.Location = new System.Drawing.Point(5, 5); 131 this.txtSearch.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 132 this.txtSearch.MaxValue = new decimal(new int[] { 133 1000000, 134 0, 135 0, 136 0}); 137 this.txtSearch.MinValue = new decimal(new int[] { 138 1000000, 139 0, 140 0, 141 -2147483648}); 142 this.txtSearch.Name = "txtSearch"; 143 this.txtSearch.Padding = new System.Windows.Forms.Padding(5); 144 this.txtSearch.PromptColor = System.Drawing.Color.Gray; 145 this.txtSearch.PromptFont = new System.Drawing.Font("微軟雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 146 this.txtSearch.PromptText = "輸入內容模糊搜索"; 147 this.txtSearch.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(220)))), ((int)(((byte)(220))))); 148 this.txtSearch.RectWidth = 1; 149 this.txtSearch.RegexPattern = ""; 150 this.txtSearch.Size = new System.Drawing.Size(437, 42); 151 this.txtSearch.TabIndex = 0; 152 // 153 // ucSplitLine_V2 154 //