欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-17 19:43:16
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......

前提

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

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

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

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

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

nuget

install-package hzh_controls

目录

用处及效果

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

准备工作

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

开始

添加一个用户控件ucdropdownbtn,继承自ucbtnimg

处理一些属性

 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 }
 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 }

 

最后的话

如果你喜欢的话,请到  点个星星吧