(四十八)c#Winform自定义控件-下拉按钮-HZHControls

官网

http://www.hzhcontrols.comhtml

前提

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

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

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitc#

若是以为写的还行,请点个 star 支持一下吧编辑器

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

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

NuGet

Install-Package HZH_Controls

目录

http://www.javashuo.com/article/p-hacmmtru-mw.htmlspa

用处及效果

准备工做

这个控件将继承自(三)c#Winform自定义控件-有图标的按钮,如不了解,请移步查看设计

开始

添加一个用户控件UCDropDownBtn,继承自UCBtnImgcode

处理一些属性

 1 Forms.FrmAnchor _frmAnchor;  2         private int _dropPanelHeight = -1;  3         public new event EventHandler BtnClick;  4         [Description("下拉框高度"), Category("自定义")]  5         public int DropPanelHeight  6  {  7             get { return _dropPanelHeight; }  8             set { _dropPanelHeight = value; }  9  } 10         private string[] btns ; 11         [Description("按钮"), Category("自定义")] 12         public string[] Btns 13  { 14             get { return btns; } 15             set { btns = value; } 16  } 17         [Obsolete("再也不可用的属性")] 18         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 19         public override Image Image 20  { 21             get; 22             set; 23  } 24         [Obsolete("再也不可用的属性")] 25         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 26         public override ContentAlignment ImageAlign 27  { 28             get; 29             set; 30         }

点击时候显示下拉框

 1 void UCDropDownBtn_BtnClick(object sender, EventArgs e)  2  {  3             if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)  4  {  5 
 6                 if (Btns != null && Btns.Length > 0)  7  {  8                     int intRow = 0;  9                     int intCom = 1; 10                     var p = this.PointToScreen(this.Location); 11                     while (true) 12  { 13                         int intScreenHeight = Screen.PrimaryScreen.Bounds.Height; 14                         if ((p.Y + this.Height + Btns.Length / intCom * 50 < intScreenHeight || p.Y - Btns.Length / intCom * 50 > 0) 15                             && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight))) 16  { 17                             intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0); 18                             break; 19  } 20                         intCom++; 21  } 22                     UCTimePanel ucTime = new UCTimePanel(); 23                     ucTime.IsShowBorder = true; 24                     int intWidth = this.Width / intCom; 25 
26                     Size size = new Size(intCom * intWidth, intRow * 50); 27                     ucTime.Size = size; 28                     ucTime.FirstEvent = true; 29                     ucTime.SelectSourceEvent += ucTime_SelectSourceEvent; 30                     ucTime.Row = intRow; 31                     ucTime.Column = intCom; 32 
33                     List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>(); 34                     foreach (var item in Btns) 35  { 36                         lst.Add(new KeyValuePair<string, string>(item, item)); 37  } 38                     ucTime.Source = lst; 39 
40                     _frmAnchor = new Forms.FrmAnchor(this, ucTime); 41                     _frmAnchor.Load += (a, b) => { (a as Form).Size = size; }; 42 
43                     _frmAnchor.Show(this.FindForm()); 44 
45  } 46  } 47             else
48  { 49  _frmAnchor.Close(); 50  } 51         }

处理一下按钮事件

 1 void ucTime_SelectSourceEvent(object sender, EventArgs e)  2  {  3             if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)  4  {  5  _frmAnchor.Close();  6 
 7                 if (BtnClick != null)  8  {  9  BtnClick(sender.ToString(), e); 10  } 11  } 12         }

完整代码

 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.Btn  11 {  12     [DefaultEvent("BtnClick")]  13     public partial class UCDropDownBtn : UCBtnImg  14  {  15  Forms.FrmAnchor _frmAnchor;  16         private int _dropPanelHeight = -1;  17         public new event EventHandler BtnClick;  18         [Description("下拉框高度"), Category("自定义")]  19         public int DropPanelHeight  20  {  21             get { return _dropPanelHeight; }  22             set { _dropPanelHeight = value; }  23  }  24         private string[] btns ;  25         [Description("按钮"), Category("自定义")]  26         public string[] Btns  27  {  28             get { return btns; }  29             set { btns = value; }  30  }  31         [Obsolete("再也不可用的属性")]  32         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]  33         public override Image Image  34  {  35             get;  36             set;  37  }  38         [Obsolete("再也不可用的属性")]  39         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]  40         public override ContentAlignment ImageAlign  41  {  42             get;  43             set;  44  }  45 
 46         public UCDropDownBtn()  47  {  48  InitializeComponent();  49             IsShowTips = false;  50             this.lbl.Image=Properties.Resources.ComboBox;  51             this.lbl.ImageAlign = ContentAlignment.MiddleRight;  52             base.BtnClick += UCDropDownBtn_BtnClick;  53  }  54 
 55         void UCDropDownBtn_BtnClick(object sender, EventArgs e)  56  {  57             if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)  58  {  59 
 60                 if (Btns != null && Btns.Length > 0)  61  {  62                     int intRow = 0;  63                     int intCom = 1;  64                     var p = this.PointToScreen(this.Location);  65                     while (true)  66  {  67                         int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;  68                         if ((p.Y + this.Height + Btns.Length / intCom * 50 < intScreenHeight || p.Y - Btns.Length / intCom * 50 > 0)  69                             && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight)))  70  {  71                             intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0);  72                             break;  73  }  74                         intCom++;  75  }  76                     UCTimePanel ucTime = new UCTimePanel();  77                     ucTime.IsShowBorder = true;  78                     int intWidth = this.Width / intCom;  79 
 80                     Size size = new Size(intCom * intWidth, intRow * 50);  81                     ucTime.Size = size;  82                     ucTime.FirstEvent = true;  83                     ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;  84                     ucTime.Row = intRow;  85                     ucTime.Column = intCom;  86 
 87                     List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();  88                     foreach (var item in Btns)  89  {  90                         lst.Add(new KeyValuePair<string, string>(item, item));  91  }  92                     ucTime.Source = lst;  93 
 94                     _frmAnchor = new Forms.FrmAnchor(this, ucTime);  95                     _frmAnchor.Load += (a, b) => { (a as Form).Size = size; };  96 
 97                     _frmAnchor.Show(this.FindForm());  98 
 99  } 100  } 101             else
102  { 103  _frmAnchor.Close(); 104  } 105  } 106         void ucTime_SelectSourceEvent(object sender, EventArgs e) 107  { 108             if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible) 109  { 110  _frmAnchor.Close(); 111 
112                 if (BtnClick != null) 113  { 114  BtnClick(sender.ToString(), e); 115  } 116  } 117  } 118  } 119 }
View Code
 1 namespace HZH_Controls.Controls.Btn  2 {  3     partial class UCDropDownBtn  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             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCDropDownBtn)); 32             this.SuspendLayout(); 33             // 
34             // lbl 35             // 
36             this.lbl.Font = new System.Drawing.Font("微软雅黑", 14F); 37             this.lbl.ForeColor = System.Drawing.Color.White; 38             this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleRight; 39             this.lbl.ImageList = null; 40             this.lbl.Text = "自定义按钮"; 41             this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 42             // 
43             // UCDropDownBtn 44             // 
45             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 46             this.BtnFont = new System.Drawing.Font("微软雅黑", 14F); 47             this.BtnForeColor = System.Drawing.Color.White; 48             this.ForeColor = System.Drawing.Color.White; 49             this.Image = ((System.Drawing.Image)(resources.GetObject("$this.Image"))); 50             this.ImageAlign = System.Drawing.ContentAlignment.MiddleRight; 51             this.Margin = new System.Windows.Forms.Padding(2); 52             this.Name = "UCDropDownBtn"; 53             this.ResumeLayout(false); 54 
55  } 56 
57         #endregion
58  } 59 }
View Code

 

最后的话

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

原文出处:https://www.cnblogs.com/bfyx/p/11417291.html

相关文章
相关标签/搜索