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

前提

入行已经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://blog.csdn.net/kwwwvagaa/article/details/100586547

用处及效果

准备工作

这个控件将继承自(三)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         }

复制代码

完整代码

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.Btn
{
    [DefaultEvent("BtnClick")]
    public partial class UCDropDownBtn : UCBtnImg
    {
        Forms.FrmAnchor _frmAnchor;
        private int _dropPanelHeight = -1;
        public new event EventHandler BtnClick;
        [Description("下拉框高度"), Category("自定义")]
        public int DropPanelHeight
        {
            get { return _dropPanelHeight; }
            set { _dropPanelHeight = value; }
        }
        private string[] btns ;
        [Description("按钮"), Category("自定义")]
        public string[] Btns
        {
            get { return btns; }
            set { btns = value; }
        }
        [Obsolete("不再可用的属性")]
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override Image Image
        {
            get;
            set;
        }
        [Obsolete("不再可用的属性")]
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override ContentAlignment ImageAlign
        {
            get;
            set;
        }

        public UCDropDownBtn()
        {
            InitializeComponent();
            IsShowTips = false;
            this.lbl.Image=Properties.Resources.ComboBox;
            this.lbl.ImageAlign = ContentAlignment.MiddleRight;
            base.BtnClick += UCDropDownBtn_BtnClick;
        }

        void UCDropDownBtn_BtnClick(object sender, EventArgs e)
        {
            if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
            {

                if (Btns != null && Btns.Length > 0)
                {
                    int intRow = 0;
                    int intCom = 1;
                    var p = this.PointToScreen(this.Location);
                    while (true)
                    {
                        int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
                        if ((p.Y + this.Height + Btns.Length / intCom * 50 < intScreenHeight || p.Y - Btns.Length / intCom * 50 > 0)
                            && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight)))
                        {
                            intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0);
                            break;
                        }
                        intCom++;
                    }
                    UCTimePanel ucTime = new UCTimePanel();
                    ucTime.IsShowBorder = true;
                    int intWidth = this.Width / intCom;

                    Size size = new Size(intCom * intWidth, intRow * 50);
                    ucTime.Size = size;
                    ucTime.FirstEvent = true;
                    ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
                    ucTime.Row = intRow;
                    ucTime.Column = intCom;

                    List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
                    foreach (var item in Btns)
                    {
                        lst.Add(new KeyValuePair<string, string>(item, item));
                    }
                    ucTime.Source = lst;

                    _frmAnchor = new Forms.FrmAnchor(this, ucTime);
                    _frmAnchor.Load += (a, b) => { (a as Form).Size = size; };

                    _frmAnchor.Show(this.FindForm());

                }
            }
            else
            {
                _frmAnchor.Close();
            }
        }
        void ucTime_SelectSourceEvent(object sender, EventArgs e)
        {
            if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
            {
                _frmAnchor.Close();

                if (BtnClick != null)
                {
                    BtnClick(sender.ToString(), e);
                }
            }
        }
    }
}
namespace HZH_Controls.Controls.Btn
{
    partial class UCDropDownBtn
    {
        /// <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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCDropDownBtn));
            this.SuspendLayout();
            // 
            // lbl
            // 
            this.lbl.Font = new System.Drawing.Font("微软雅黑", 14F);
            this.lbl.ForeColor = System.Drawing.Color.White;
            this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.lbl.ImageList = null;
            this.lbl.Text = "自定义按钮";
            this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // UCDropDownBtn
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BtnFont = new System.Drawing.Font("微软雅黑", 14F);
            this.BtnForeColor = System.Drawing.Color.White;
            this.ForeColor = System.Drawing.Color.White;
            this.Image = ((System.Drawing.Image)(resources.GetObject("$this.Image")));
            this.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.Margin = new System.Windows.Forms.Padding(2);
            this.Name = "UCDropDownBtn";
            this.ResumeLayout(false);

        }

        #endregion
    }
}

最后的话

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值