(四十九)c#Winform自定义控件-下拉框(表格)
2019-08-28 17:46
2331 查看
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace HZH_Controls.Controls.ComboBox { [ToolboxItem(false)] public partial class UCComboxGridPanel : UserControl { [Description("项点击事件"), Category("自定义")] public event DataGridViewEventHandler ItemClick; private Type m_rowType = typeof(UCDataGridViewRow); public Type RowType { get { return m_rowType; } set { m_rowType = value; this.ucDataGridView1.RowType = m_rowType; } } private List<DataGridViewColumnEntity> m_columns = null; public List<DataGridViewColumnEntity> Columns { get { return m_columns; } set { m_columns = value; this.ucDataGridView1.Columns = value; } } private List<object> m_dataSource = null; public List<object> DataSource { get { return m_dataSource; } set { m_dataSource = value; } } private string strLastSearchText = string.Empty; 49 UCPagerControl m_page = new UCPagerControl(); public UCComboxGridPanel() { InitializeComponent(); this.ucDataGridView1.Page = m_page; this.ucDataGridView1.IsAutoHeight = false; this.txtSearch.txtInput.TextChanged += txtInput_TextChanged; this.ucDataGridView1.ItemClick += ucDataGridView1_ItemClick; } void ucDataGridView1_ItemClick(object sender, DataGridViewEventArgs e) { if (ItemClick != null) { ItemClick((sender as IDataGridViewRow).DataSource, null); } } void txtInput_TextChanged(object sender, EventArgs e) { timer1.Enabled = false; timer1.Enabled = true; } private void UCComboxGridPanel_Load(object sender, EventArgs e) { m_page.DataSource = m_dataSource; this.ucDataGridView1.DataSource = m_page.GetCurrentSource(); } private void timer1_Tick(object sender, EventArgs e) { if (strLastSearchText == txtSearch.InputText) { timer1.Enabled = false; } else { strLastSearchText = txtSearch.InputText; Search(txtSearch.InputText); } } private void Search(string strText) { m_page.StartIndex = 0; if (!string.IsNullOrEmpty(strText)) { strText = strText.ToLower(); 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))); m_page.DataSource = lst; } else { m_page.DataSource = m_dataSource; } m_page.Reload(); } } }View Code
namespace HZH_Controls.Controls.ComboBox { partial class UCComboxGridPanel { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.panel1 = new System.Windows.Forms.Panel(); this.panel2 = new System.Windows.Forms.Panel(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.ucControlBase1 = new HZH_Controls.Controls.UCControlBase(); this.ucDataGridView1 = new HZH_Controls.Controls.UCDataGridView(); this.txtSearch = new HZH_Controls.Controls.UCTextBoxEx(); this.ucSplitLine_V2 = new HZH_Controls.Controls.UCSplitLine_V(); this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V(); this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H(); this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H(); this.panel1.SuspendLayout(); this.ucControlBase1.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.Add(this.ucCon 3ff8 trolBase1); this.panel1.Controls.Add(this.panel2); this.panel1.Controls.Add(this.txtSearch); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(1, 1); this.panel1.Name = "panel1"; this.panel1.Padding = new System.Windows.Forms.Padding(5); this.panel1.Size = new System.Drawing.Size(447, 333); this.panel1.TabIndex = 4; // // panel2 // this.panel2.Dock = System.Windows.Forms.DockStyle.Top; this.panel2.Location = new System.Drawing.Point(5, 47); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(437, 15); this.panel2.TabIndex = 1; // // timer1 // this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // ucControlBase1 // this.ucControlBase1.ConerRadius = 5; this.ucControlBase1.Controls.Add(this.ucDataGridView1); this.ucControlBase1.Dock = System.Windows.Forms.DockStyle.Fill; this.ucControlBase1.FillColor = System.Drawing.Color.Transparent; this.ucControlBase1.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); this.ucControlBase1.IsRadius = false; this.ucControlBase1.IsShowRect = true; this.ucControlBase1.Location = new System.Drawing.Point(5, 62); this.ucControlBase1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.ucControlBase1.Name = "ucControlBase1"; this.ucControlBase1.Padding = new System.Windows.Forms.Padding(5); this.ucControlBase1.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(220)))), ((int)(((byte)(220))))); this.ucControlBase1.RectWidth = 1; this.ucControlBase1.Size = new System.Drawing.Size(437, 266); this.ucControlBase1.TabIndex = 2; this.ucControlBase1.TabStop = false; // // ucDataGridView1 // this.ucDataGridView1.BackColor = System.Drawing.Color.White; this.ucDataGridView1.Columns = null; this.ucDataGridView1.DataSource = null; this.ucDataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.ucDataGridView1.HeadFont = new System.Drawing.Font("微软雅黑", 12F); this.ucDataGridView1.HeadHeight = 40; this.ucDataGridView1.HeadPadingLeft = 0; this.ucDataGridView1.HeadTextColor = System.Drawing.Color.Black; this.ucDataGridView1.IsAutoHeight = false; this.ucDataGridView1.IsShowCheckBox = false; this.ucDataGridView1.IsShowHead = true; this.ucDataGridView1.Location = new System.Drawing.Point(5, 5); this.ucDataGridView1.Name = "ucDataGridView1"; this.ucDataGridView1.Page = null; this.ucDataGridView1.RowHeight = 30; this.ucDataGridView1.RowType = typeof(HZH_Controls.Controls.UCDataGridViewRow); this.ucDataGridView1.Size = new System.Drawing.Size(427, 256); this.ucDataGridView1.TabIndex = 0; this.ucDataGridView1.TabStop = false; // // txtSearch // this.txtSearch.BackColor = System.Drawing.Color.Transparent; this.txtSearch.ConerRadius = 5; this.txtSearch.Cursor = System.Windows.Forms.Cursors.IBeam; this.txtSearch.DecLength = 2; this.txtSearch.Dock = System.Windows.Forms.DockStyle.Top; this.txtSearch.FillColor = System.Drawing.Color.Empty; this.txtSearch.Font = new System.Drawing.Font("微软雅黑", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); this.txtSearch.InputText = ""; this.txtSearch.InputType = HZH_Controls.TextInputType.NotControl; this.txtSearch.IsFocusColor = true; this.txtSearch.IsRadius = true; 3ff8 this.txtSearch.IsShowClearBtn = true; this.txtSearch.IsShowKeyboard = false; this.txtSearch.IsShowRect = true; this.txtSearch.IsShowSearchBtn = false; this.txtSearch.KeyBoardType = HZH_Controls.Controls.KeyBoardType.UCKeyBorderAll_EN; this.txtSearch.Location = new System.Drawing.Point(5, 5); this.txtSearch.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.txtSearch.MaxValue = new decimal(new int[] { 1000000, 0, 0, 0}); this.txtSearch.MinValue = new decimal(new int[] { 1000000, 0, 0, -2147483648}); this.txtSearch.Name = "txtSearch"; this.txtSearch.Padding = new System.Windows.Forms.Padding(5); this.txtSearch.PromptColor = System.Drawing.Color.Gray; this.txtSearch.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); this.txtSearch.PromptText = "输入内容模糊搜索"; this.txtSearch.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(220)))), ((int)(((byte)(220))))); this.txtSearch.RectWidth = 1; this.txtSearch.RegexPattern = ""; this.txtSearch.Size = new System.Drawing.Size(437, 42); this.txtSearch.TabIndex = 0; // // ucSplitLine_V2 // this.ucSplitLine_V2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232))))); this.ucSplitLine_V2.Dock = System.Windows.Forms.DockStyle.Right; this.ucSplitLine_V2.Location = new System.Drawing.Point(448, 1); this.ucSplitLine_V2.Name = "ucSplitLine_V2"; this.ucSplitLine_V2.Size = new System.Drawing.Size(1, 333); this.ucSplitLine_V2.TabIndex = 3; this.ucSplitLine_V2.TabStop = false; // // ucSplitLine_V1 // this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232))))); this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left; this.ucSplitLine_V1.Location = new System.Drawing.Point(0, 1); this.ucSplitLine_V1.Name = "ucSplitLine_V1"; this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 333); this.ucSplitLine_V1.TabIndex = 2; this.ucSplitLine_V1.TabStop = false; // // ucSplitLine_H2 // this.ucSplitLine_H2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232))))); this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom; this.ucSplitLine_H2.Location = new System.Drawing.Point(0, 334); this.ucSplitLine_H2.Name = "ucSplitLine_H2"; this.ucSplitLine_H2.Size = new System.Drawing.Size(449, 1); this.ucSplitLine_H2.TabIndex = 1; this.ucSplitLine_H2.TabStop = false; // // ucSplitLine_H1 // this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232))))); this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top; this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 0); this.ucSplitLine_H1.Name = "ucSplitLine_H1"; this.ucSplitLine_H1.Size = new System.Drawing.Size(449, 1); this.ucSplitLine_H1.TabIndex = 0; this.ucSplitLine_H1.TabStop = false; // // UCComboxGridPanel // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.BackColor = System.Drawing.Color.White; this.Controls.Add(this.panel1); this.Controls.Add(this.ucSplitLine_V2); this.Controls.Add(this.ucSplitLine_V1); this.Controls.Add(this.ucSplitLine_H2); this.Controls.Add(this.ucSplitLine_H1); this.Name = "UCComboxGridPanel"; this.Size = new System.Drawing.Size(449, 335); this.Load += new System.EventHandler(this.UCComboxGridPanel_Load); this.panel1.ResumeLayout(false); this.ucControlBase1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private UCSplitLine_H ucSplitLine_H1; private UCSplitLine_H ucSplitLine_H2; private UCSplitLine_V ucSplitLine_V1; private UCSplitLine_V ucSplitLine_V2; private System.Windows.Forms.Panel panel1; private UCControlBase ucControlBase1; private UCDataGridView ucDataGridView1; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Timer timer1; private UCTextBoxEx txtSearch; } }View Code 以上,弹出面板完成,下面就是下拉控件了
添加一个用户控件UCComboxGrid,继承UCCombox
一些属性
private Type m_rowType = typeof(UCDataGridViewRow); [Description("表格行类型"), Category("自定义")] public Type GridRowType { get { return m_rowType; } set { m_rowType = value; } } int intWidth = 0; private List<DataGridViewColumnEntity> m_columns = null; [Description("表格列"), Category("自定义")] public List<DataGridViewColumnEntity> GridColumns { get { return m_columns; } set { m_columns = value; if (value != null) intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width)); } } private List<object> m_dataSource = null; [Description("表格数据源"), Category("自定义")] public List<object> GridDataSource { get { return m_dataSource; } set { m_dataSource = value; } } private string m_textField; [Description("显示值字段名称"), Category("自定义")] public string TextField { get { return m_textField; } set { m_textField = value; SetText(); } } [Obsolete("不再可用的属性")] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] private new ComboBoxStyle BoxStyle { get; set; } private object selectSource = null; [Description("选中的数据源"), Category("自定义")] public object SelectSource { get { return selectSource; } set { selectSource = value; SetText(); if (SelectedChangedEvent != null) { SelectedChangedEvent(value, null); } } } [Description("选中数据源改变事件"), Category("自定义")] public new event EventHandler SelectedChangedEvent;
重写点击事件来处理弹出
protected override void click_MouseDown(object sender, MouseEventArgs e) { if (m_columns == null || m_columns.Count <= 0) return; if (m_ucPanel == null) { var p = this.Parent.PointToScreen(this.Location); int intScreenHeight = Screen.PrimaryScreen.Bounds.Height; int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height); intHeight -= 100; m_ucPanel = new UCComboxGridPanel(); m_ucPanel.ItemClick += m_ucPanel_ItemClick; m_ucPanel.Height = intHeight; m_ucPanel.Width = intWidth; m_ucPanel.Columns = m_columns; m_ucPanel.RowType = m_rowType; if (m_dataSource != null && m_dataSource.Count > 0) { int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height); m_ucPanel.Height = _intHeight; } } m_ucPanel.DataSource = m_dataSource; if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false) { _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel); _frmAnchor.Show(this.FindForm()); } } void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e) { _frmAnchor.Hide(); SelectSource = sender; } private void SetText() { if (!string.IsNullOrEmpty(m_textField) && selectSource != null) { var pro = selectSource.GetType().GetProperty(m_textField); if (pro != null) { TextValue = pro.GetValue(selectSource, null).ToStringExt(); } } }
完整代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HZH_Controls.Controls; namespace HZH_Controls.Controls.ComboBox { public partial class UCComboxGrid : UCCombox { private Type m_rowType = typeof(UCDataGridViewRow); [Description("表格行类型"), Category("自定义")] public Type GridRowType { get { return m_rowType; } set { m_rowType = value; } } int intWidth = 0; private List<DataGridViewColumnEntity> m_columns = null; [Description("表格列"), Category("自定义")] public List<DataGridViewColumnEntity> GridColumns { get { return m_columns; } set { m_columns = value; if (value != null) intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width)); } } private List<object> m_dataSource = null; [Description("表格数据源"), Category("自定义")] public List<object> GridDataSource { get { return m_dataSource; } set { m_dataSource = value; } } private string m_textField; [Description("显示值字段名称"), Category("自定义")] public string TextField { get { return m_textField; } set { m_textField = value; SetText(); } } [Obsolete("不再可用的属性")] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 65 private new ComboBoxStyle BoxStyle { get; set; } private object selectSource = null; [Description("选中的数据源"), Category("自定义")] public object SelectSource { get { return selectSource; } set { selectSource = value; SetText(); if (SelectedChangedEvent != null) { SelectedChangedEvent(value, null); } } } [Description("选中数据源改变事件"), Category("自定义")] public new event EventHandler SelectedChangedEvent; public UCComboxGrid() { InitializeComponent(); } UCComboxGridPanel m_ucPanel = null; Forms.FrmAnchor _frmAnchor; protected override void click_MouseDown(object sender, MouseEventArgs e) { if (m_columns == null || m_columns.Count <= 0) return; if (m_ucPanel == null) { var p = this.Parent.PointToScreen(this.Location); int intScreenHeight = Screen.PrimaryScreen.Bounds.Height; int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height); intHeight -= 100; m_ucPanel = new UCComboxGridPanel(); m_ucPanel.ItemClick += m_ucPanel_ItemClick; m_ucPanel.Height = intHeight; m_ucPanel.Width = intWidth; m_ucPanel.Columns = m_columns; m_ucPanel.RowType = m_rowType; if (m_dataSource != null && m_dataSource.Count > 0) { int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height); m_ucPanel.Height = _intHeight; } } m_ucPanel.DataSource = m_dataSource; if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false) { _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel); _frmAnchor.Show(this.FindForm()); } } void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e) { _frmAnchor.Hide(); SelectSource = sender; } private void SetText() { if (!string.IsNullOrEmpty(m_textField) && selectSource != null) { var pro = selectSource.GetType().GetProperty(m_textField); if (pro != null) { TextValue = pro.GetValue(selectSource, null).ToStringExt(); } } } } }View Code
namespace HZH_Controls.Controls.ComboBox { partial class UCComboxGrid { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // txtInput // this.txtInput.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240))))); // // UCComboxGrid // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.BackColor = System.Drawing.Color.Transparent; this.BoxStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240))))); this.Name = "UCComboxGrid"; this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240))))); this.ResumeLayout(false); this.PerformLayout(); } #endregion } }View Code
以上就是全部东西了
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
相关文章推荐
- (四十七)c#Winform自定义控件-树表格(treeGrid)
- C#表格导入实例(Winform也可以)直接复制可用
- C# WinForm自定义控件整理(转载)
- C# Winform读取Excel表格
- C# winform 开发之Excel操作(二)------读取Excel表格 (续)
- C# winform自定义控件在工具箱中显示和属性在属性面板中显示
- C#学习(一)winform程序中使用表格
- (四十五)c#Winform自定义控件-水波图表
- c#创建自定义控件在winform上添加该控件属性为空白
- C# WinForm自定义控件响应键盘事件
- [转]C# WinForm自定义控件整理
- C#WinForm自定义控件自动停靠
- C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
- C#winform自定义控件模拟设计时界面鼠标移动和调节大小、选中效果
- C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
- c# winform textBox的下拉框是制作, 如google的搜索栏.
- C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
- C#WinForm自定义控件样式(textBox,comboBox,form,radiobutton,checkbox)
- C# 自定义控件制作和使用实例(winform)
- 【转】C# WinForm自定义控件整理