zoukankan      html  css  js  c++  java
  • (四十九)c#Winform自定义控件-下拉框(表格)-HZHControls

    官网

    http://www.hzhcontrols.com

    前提

    入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

    GitHub:https://github.com/kwwwvagaa/NetWinformControl

    码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

    如果觉得写的还行,请点个 star 支持一下吧

    欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

    麻烦博客下方点个【推荐】,谢谢

    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自定义控件-下拉框

    (三十二)c#Winform自定义控件-表格

    如果你想显示树形结构,请移步查看

    (四十七)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             // 
    155             this.ucSplitLine_V2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
    156             this.ucSplitLine_V2.Dock = System.Windows.Forms.DockStyle.Right;
    157             this.ucSplitLine_V2.Location = new System.Drawing.Point(448, 1);
    158             this.ucSplitLine_V2.Name = "ucSplitLine_V2";
    159             this.ucSplitLine_V2.Size = new System.Drawing.Size(1, 333);
    160             this.ucSplitLine_V2.TabIndex = 3;
    161             this.ucSplitLine_V2.TabStop = false;
    162             // 
    163             // ucSplitLine_V1
    164             // 
    165             this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
    166             this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
    167             this.ucSplitLine_V1.Location = new System.Drawing.Point(0, 1);
    168             this.ucSplitLine_V1.Name = "ucSplitLine_V1";
    169             this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 333);
    170             this.ucSplitLine_V1.TabIndex = 2;
    171             this.ucSplitLine_V1.TabStop = false;
    172             // 
    173             // ucSplitLine_H2
    174             // 
    175             this.ucSplitLine_H2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
    176             this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom;
    177             this.ucSplitLine_H2.Location = new System.Drawing.Point(0, 334);
    178             this.ucSplitLine_H2.Name = "ucSplitLine_H2";
    179             this.ucSplitLine_H2.Size = new System.Drawing.Size(449, 1);
    180             this.ucSplitLine_H2.TabIndex = 1;
    181             this.ucSplitLine_H2.TabStop = false;
    182             // 
    183             // ucSplitLine_H1
    184             // 
    185             this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
    186             this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
    187             this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 0);
    188             this.ucSplitLine_H1.Name = "ucSplitLine_H1";
    189             this.ucSplitLine_H1.Size = new System.Drawing.Size(449, 1);
    190             this.ucSplitLine_H1.TabIndex = 0;
    191             this.ucSplitLine_H1.TabStop = false;
    192             // 
    193             // UCComboxGridPanel
    194             // 
    195             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
    196             this.BackColor = System.Drawing.Color.White;
    197             this.Controls.Add(this.panel1);
    198             this.Controls.Add(this.ucSplitLine_V2);
    199             this.Controls.Add(this.ucSplitLine_V1);
    200             this.Controls.Add(this.ucSplitLine_H2);
    201             this.Controls.Add(this.ucSplitLine_H1);
    202             this.Name = "UCComboxGridPanel";
    203             this.Size = new System.Drawing.Size(449, 335);
    204             this.Load += new System.EventHandler(this.UCComboxGridPanel_Load);
    205             this.panel1.ResumeLayout(false);
    206             this.ucControlBase1.ResumeLayout(false);
    207             this.ResumeLayout(false);
    208 
    209         }
    210 
    211         #endregion
    212 
    213         private UCSplitLine_H ucSplitLine_H1;
    214         private UCSplitLine_H ucSplitLine_H2;
    215         private UCSplitLine_V ucSplitLine_V1;
    216         private UCSplitLine_V ucSplitLine_V2;
    217         private System.Windows.Forms.Panel panel1;
    218         private UCControlBase ucControlBase1;
    219         private UCDataGridView ucDataGridView1;
    220         private System.Windows.Forms.Panel panel2;
    221         private System.Windows.Forms.Timer timer1;
    222         private UCTextBoxEx txtSearch;
    223     }
    224 }
    View Code

    以上,弹出面板完成,下面就是下拉控件了

    添加一个用户控件UCComboxGrid,继承UCCombox

    一些属性

     1  private Type m_rowType = typeof(UCDataGridViewRow);
     2 
     3         [Description("表格行类型"), Category("自定义")]
     4         public Type GridRowType
     5         {
     6             get { return m_rowType; }
     7             set
     8             {
     9                 m_rowType = value;
    10             }
    11         }
    12         int intWidth = 0;
    13 
    14         private List<DataGridViewColumnEntity> m_columns = null;
    15 
    16         [Description("表格列"), Category("自定义")]
    17         public List<DataGridViewColumnEntity> GridColumns
    18         {
    19             get { return m_columns; }
    20             set
    21             {
    22                 m_columns = value;
    23                 if (value != null)
    24                     intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width));
    25             }
    26         }
    27         private List<object> m_dataSource = null;
    28 
    29         [Description("表格数据源"), Category("自定义")]
    30         public List<object> GridDataSource
    31         {
    32             get { return m_dataSource; }
    33             set { m_dataSource = value; }
    34         }
    35 
    36         private string m_textField;
    37 
    38         [Description("显示值字段名称"), Category("自定义")]
    39         public string TextField
    40         {
    41             get { return m_textField; }
    42             set
    43             {
    44                 m_textField = value;
    45                 SetText();
    46             }
    47         }
    48         [Obsolete("不再可用的属性")]
    49         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    50         private new ComboBoxStyle BoxStyle
    51         {
    52             get;
    53             set;
    54         }
    55         private object selectSource = null;
    56 
    57         [Description("选中的数据源"), Category("自定义")]
    58         public object SelectSource
    59         {
    60             get { return selectSource; }
    61             set
    62             {
    63                 selectSource = value;
    64                 SetText();
    65                 if (SelectedChangedEvent != null)
    66                 {
    67                     SelectedChangedEvent(value, null);
    68                 }
    69             }
    70         }
    71 
    72  [Description("选中数据源改变事件"), Category("自定义")]
    73         public new event EventHandler SelectedChangedEvent;

    重写点击事件来处理弹出

     1  protected override void click_MouseDown(object sender, MouseEventArgs e)
     2         {
     3             if (m_columns == null || m_columns.Count <= 0)
     4                 return;
     5             if (m_ucPanel == null)
     6             {
     7                 var p = this.Parent.PointToScreen(this.Location);
     8                 int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
     9                 int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height);
    10                 intHeight -= 100;
    11                 m_ucPanel = new UCComboxGridPanel();
    12                 m_ucPanel.ItemClick += m_ucPanel_ItemClick;
    13                 m_ucPanel.Height = intHeight;
    14                 m_ucPanel.Width = intWidth;
    15                 m_ucPanel.Columns = m_columns;
    16                 m_ucPanel.RowType = m_rowType;
    17                 if (m_dataSource != null && m_dataSource.Count > 0)
    18                 {
    19                     int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height);
    20                     m_ucPanel.Height = _intHeight;
    21                 }
    22             }
    23             m_ucPanel.DataSource = m_dataSource;
    24             if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
    25             {
    26                 _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel);
    27                 _frmAnchor.Show(this.FindForm());
    28             }
    29 
    30         }
    31 
    32         void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e)
    33         {
    34             _frmAnchor.Hide();
    35             SelectSource = sender;
    36         }
    37 
    38         private void SetText()
    39         {
    40             if (!string.IsNullOrEmpty(m_textField) && selectSource != null)
    41             {
    42                 var pro = selectSource.GetType().GetProperty(m_textField);
    43                 if (pro != null)
    44                 {
    45                     TextValue = pro.GetValue(selectSource, null).ToStringExt();
    46                 }
    47             }
    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 using HZH_Controls.Controls;
     10 
     11 namespace HZH_Controls.Controls.ComboBox
     12 {
     13     public partial class UCComboxGrid : UCCombox
     14     {
     15 
     16         private Type m_rowType = typeof(UCDataGridViewRow);
     17 
     18         [Description("表格行类型"), Category("自定义")]
     19         public Type GridRowType
     20         {
     21             get { return m_rowType; }
     22             set
     23             {
     24                 m_rowType = value;
     25             }
     26         }
     27         int intWidth = 0;
     28 
     29         private List<DataGridViewColumnEntity> m_columns = null;
     30 
     31         [Description("表格列"), Category("自定义")]
     32         public List<DataGridViewColumnEntity> GridColumns
     33         {
     34             get { return m_columns; }
     35             set
     36             {
     37                 m_columns = value;
     38                 if (value != null)
     39                     intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width));
     40             }
     41         }
     42         private List<object> m_dataSource = null;
     43 
     44         [Description("表格数据源"), Category("自定义")]
     45         public List<object> GridDataSource
     46         {
     47             get { return m_dataSource; }
     48             set { m_dataSource = value; }
     49         }
     50 
     51         private string m_textField;
     52 
     53         [Description("显示值字段名称"), Category("自定义")]
     54         public string TextField
     55         {
     56             get { return m_textField; }
     57             set
     58             {
     59                 m_textField = value;
     60                 SetText();
     61             }
     62         }
     63         [Obsolete("不再可用的属性")]
     64         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
     65         private new ComboBoxStyle BoxStyle
     66         {
     67             get;
     68             set;
     69         }
     70         private object selectSource = null;
     71 
     72         [Description("选中的数据源"), Category("自定义")]
     73         public object SelectSource
     74         {
     75             get { return selectSource; }
     76             set
     77             {
     78                 selectSource = value;
     79                 SetText();
     80                 if (SelectedChangedEvent != null)
     81                 {
     82                     SelectedChangedEvent(value, null);
     83                 }
     84             }
     85         }
     86 
     87       
     88 
     89         [Description("选中数据源改变事件"), Category("自定义")]
     90         public new event EventHandler SelectedChangedEvent;
     91         public UCComboxGrid()
     92         {
     93             InitializeComponent();
     94         }
     95         UCComboxGridPanel m_ucPanel = null;
     96         Forms.FrmAnchor _frmAnchor;
     97         protected override void click_MouseDown(object sender, MouseEventArgs e)
     98         {
     99             if (m_columns == null || m_columns.Count <= 0)
    100                 return;
    101             if (m_ucPanel == null)
    102             {
    103                 var p = this.Parent.PointToScreen(this.Location);
    104                 int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
    105                 int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height);
    106                 intHeight -= 100;
    107                 m_ucPanel = new UCComboxGridPanel();
    108                 m_ucPanel.ItemClick += m_ucPanel_ItemClick;
    109                 m_ucPanel.Height = intHeight;
    110                 m_ucPanel.Width = intWidth;
    111                 m_ucPanel.Columns = m_columns;
    112                 m_ucPanel.RowType = m_rowType;
    113                 if (m_dataSource != null && m_dataSource.Count > 0)
    114                 {
    115                     int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height);
    116                     m_ucPanel.Height = _intHeight;
    117                 }
    118             }
    119             m_ucPanel.DataSource = m_dataSource;
    120             if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
    121             {
    122                 _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel);
    123                 _frmAnchor.Show(this.FindForm());
    124             }
    125 
    126         }
    127 
    128         void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e)
    129         {
    130             _frmAnchor.Hide();
    131             SelectSource = sender;
    132         }
    133 
    134         private void SetText()
    135         {
    136             if (!string.IsNullOrEmpty(m_textField) && selectSource != null)
    137             {
    138                 var pro = selectSource.GetType().GetProperty(m_textField);
    139                 if (pro != null)
    140                 {
    141                     TextValue = pro.GetValue(selectSource, null).ToStringExt();
    142                 }
    143             }
    144         }
    145     }
    146 }
    View Code
     1 namespace HZH_Controls.Controls.ComboBox
     2 {
     3     partial class UCComboxGrid
     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.SuspendLayout();
    32             // 
    33             // txtInput
    34             // 
    35             this.txtInput.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
    36             // 
    37             // UCComboxGrid
    38             // 
    39             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
    40             this.BackColor = System.Drawing.Color.Transparent;
    41             this.BoxStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    42             this.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
    43             this.Name = "UCComboxGrid";
    44             this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
    45             this.ResumeLayout(false);
    46             this.PerformLayout();
    47 
    48         }
    49 
    50         #endregion
    51 
    52     }
    53 }
    View Code

    以上就是全部东西了

    最后的话

    如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

  • 相关阅读:
    java常量和变量的定义规则,变长参数的使用
    测试一下windowsLiveWriter
    对转换公式为LaTeX代码要注意什么
    后台登陆骨架
    为什么要把session存入数据库
    登录功能测试
    数据库快速配置
    一个小bug
    后台测试常需要的htm样式
    在分页的基础上添加删除和(查看,原理和删除一样)
  • 原文地址:https://www.cnblogs.com/bfyx/p/11425464.html
Copyright © 2011-2022 走看看