(三十三)c#Winform自定义控件-日期控件

前提

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

官网:https://www.hzhcontrols.cn

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

码云:HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~

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

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

目录

c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-优快云博客

准备工作

日期控件将分为3部分进行处理,分别是,列表、日期面板、输入控件

将用到停靠窗体和基类控件,如你还没有了解,请移步查看

(十九)c#Winform自定义控件-停靠窗体

(一)c#Winform自定义控件-基类控件

开始

添加用户控件,命名UCTimePanel

属性

复制代码

 1   public event EventHandler SelectSourceEvent;
 2         private List<KeyValuePair<string, string>> source = null;
 3         public bool FirstEvent { get; set; }
 4 
 5         public List<KeyValuePair<string, string>> Source
 6         {
 7             get { return source; }
 8             set
 9             {
10                 source = value;
11                 SetSource(value);
12             }
13         }
14 
15         private bool _IsShowBorder = false;
16 
17         public bool IsShowBorder
18         {
19             get { return _IsShowBorder; }
20             set
21             {
22                 _IsShowBorder = value;
23                 ucSplitLine_H1.Visible = value;
24                 ucSplitLine_H2.Visible = value;
25                 ucSplitLine_V1.Visible = value;
26                 ucSplitLine_V2.Visible = value;
27             }
28         }
29 
30         UCBtnExt selectBtn;
31         /// <summary>
32         /// 选中按钮
33         /// </summary>
34         public UCBtnExt SelectBtn
35         {
36             get { return selectBtn; }
37             set
38             {
39                 if (selectBtn != null && !selectBtn.IsDisposed)
40                 {
41                     selectBtn.FillColor = System.Drawing.Color.White;
42                     selectBtn.RectColor = System.Drawing.Color.White;
43                     selectBtn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
44                 }
45                 bool blnEvent = FirstEvent ? true : (selectBtn != null);
46                 selectBtn = value;
47                 if (value != null)
48                 {
49                     selectBtn.FillColor = System.Drawing.Color.FromArgb(255, 77, 59);
50                     selectBtn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
51                     selectBtn.BtnForeColor = System.Drawing.Color.White;
52                     if (blnEvent && SelectSourceEvent != null)
53                         SelectSourceEvent(selectBtn.Tag.ToStringExt(), null);
54                 }
55             }
56         }
57  private int row = 0;
58 
59         public int Row
60         {
61             get { return row; }
62             set
63             {
64                 row = value;
65                 ReloadPanel();
66             }
67         }
68 
69 
70         private int column = 0;
71 
72         public int Column
73         {
74             get { return column; }
75             set
76             {
77                 column = value;
78                 ReloadPanel();
79             }
80         }

复制代码

一些公共函数

复制代码

  1         #region 设置面板数据源
  2         /// <summary>
  3         /// 功能描述:设置面板数据源
  4         /// 作  者:HZH
  5         /// 创建日期:2019-06-25 15:02:15
  6         /// 任务编号:POS
  7         /// </summary>
  8         /// <param name="lstSource">lstSource</param>
  9         public void SetSource(List<KeyValuePair<string, string>> lstSource)
 10         {
 11             try
 12             {
 13                 ControlHelper.FreezeControl(this, true);
 14                 if (row <= 0 || column <= 0)
 15                     return;
 16                 if (Source != lstSource)
 17                     Source = lstSource;
 18                 int index = 0;
 19                 SelectBtn = null;
 20                 foreach (UCBtnExt btn in this.panMain.Controls)
 21                 {
 22                     if (lstSource != null && index < lstSource.Count)
 23                     {
 24                         btn.BtnText = lstSource[index].Value;
 25                         btn.Tag = lstSource[index].Key;
 26                         index++;
 27                     }
 28                     else
 29                     {
 30                         btn.BtnText = "";
 31                         btn.Tag = null;
 32                     }
 33                 }
 34             }
 35             finally
 36             {
 37                 ControlHelper.FreezeControl(this, false);
 38             }
 39         }
 40         #endregion
 41         /// <summary>
 42         /// 设置选中项
 43         /// </summary>
 44         /// <param name="strKey"></param>
 45         public void SetSelect(string strKey)
 46         {
 47             foreach (UCBtnExt item in this.panMain.Controls)
 48             {
 49                 if (item.Tag != null && item.Tag.ToStringExt() == strKey)
 50                 {
 51                     SelectBtn = item;
 52                     return;
 53                 }
 54             }
 55             SelectBtn = new UCBtnExt();
 56         }
 57 
 58         #region 重置面板
 59         /// <summary>
 60         /// 功能描述:重置面板
 61         /// 作  者:HZH
 62         /// 创建日期:2019-06-25 15:02:05
 63         /// 任务编号:POS
 64         /// </summary>
 65         public void ReloadPanel()
 66         {
 67             if (row <= 0 || column <= 0)
 68                 return;
 69             SelectBtn = null;
 70             this.panMain.Controls.Clear();
 71             this.panMain.ColumnCount = column;
 72             this.panMain.ColumnStyles.Clear();
 73             for (int i = 0; i < column; i++)
 74             {
 75                 this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
 76             }
 77 
 78             this.panMain.RowCount = row;
 79             this.panMain.RowStyles.Clear();
 80             for (int i = 0; i < row; i++)
 81             {
 82                 this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
 83             }
 84 
 85             for (int i = 0; i < row; i++)
 86             {
 87                 for (int j = 0; j < column; j++)
 88                 {
 89                     UCBtnExt btn = new UCBtnExt();
 90                     btn.BackColor = System.Drawing.Color.Transparent;
 91                     btn.BtnBackColor = System.Drawing.Color.Transparent;
 92                     btn.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
 93                     btn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
 94                     btn.ConerRadius = 5;
 95                     btn.Dock = DockStyle.Fill;
 96                     btn.FillColor = System.Drawing.Color.White;
 97                     btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
 98                     btn.Cursor = Cursor.Current;
 99                     btn.IsShowRect = true;
100                     btn.IsRadius = true;
101                     btn.IsShowTips = false;
102                     btn.Name = "btn_" + i + "_" + j;
103                     btn.RectColor = System.Drawing.Color.White;
104                     btn.RectWidth = 1;
105                     btn.Width = this.Width;
106                     btn.TabIndex = 0;
107                     btn.TipsText = "";
108                     btn.BtnClick += btn_BtnClick;
109                     this.panMain.Controls.Add(btn, j, i);
110                 }
111             }
112 
113             if (Source != null)
114             {
115                 SetSource(Source);
116             }
117         }
118         #endregion
119 
120         void btn_BtnClick(object sender, EventArgs e)
121         {
122             var btn = (UCBtnExt)sender;
123             if (btn.Tag == null)
124                 return;
125             SelectBtn = btn;
126         }

复制代码

全部代码

// 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCTimePanel.cs
// 创建日期:2019-08-15 15:59:56
// 功能描述:DateTime
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
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
{
    [ToolboxItem(false)]
    public partial class UCTimePanel : UserControl
    {
        public event EventHandler SelectSourceEvent;
        private List<KeyValuePair<string, string>> source = null;
        public bool FirstEvent { get; set; }

        public List<KeyValuePair<string, string>> Source
        {
            get { return source; }
            set
            {
                source = value;
                SetSource(value);
            }
        }

        private bool _IsShowBorder = false;

        public bool IsShowBorder
        {
            get { return _IsShowBorder; }
            set
            {
                _IsShowBorder = value;
                ucSplitLine_H1.Visible = value;
                ucSplitLine_H2.Visible = value;
                ucSplitLine_V1.Visible = value;
                ucSplitLine_V2.Visible = value;
            }
        }

        UCBtnExt selectBtn;
        /// <summary>
        /// 选中按钮
        /// </summary>
        public UCBtnExt SelectBtn
        {
            get { return selectBtn; }
            set
            {
                if (selectBtn != null && !selectBtn.IsDisposed)
                {
                    selectBtn.FillColor = System.Drawing.Color.White;
                    selectBtn.RectColor = System.Drawing.Color.White;
                    selectBtn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
                }
                bool blnEvent = FirstEvent ? true : (selectBtn != null);
                selectBtn = value;
                if (value != null)
                {
                    selectBtn.FillColor = System.Drawing.Color.FromArgb(255, 77, 59);
                    selectBtn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
                    selectBtn.BtnForeColor = System.Drawing.Color.White;
                    if (blnEvent && SelectSourceEvent != null)
                        SelectSourceEvent(selectBtn.Tag.ToStringExt(), null);
                }
            }
        }
        public UCTimePanel()
        {
            InitializeComponent();
            this.SizeChanged += UCTimePanel_SizeChanged;
        }

        void UCTimePanel_SizeChanged(object sender, EventArgs e)
        {

        }

        private int row = 0;

        public int Row
        {
            get { return row; }
            set
            {
                row = value;
                ReloadPanel();
            }
        }


        private int column = 0;

        public int Column
        {
            get { return column; }
            set
            {
                column = value;
                ReloadPanel();
            }
        }

        private void UCTimePanel_Load(object sender, EventArgs e)
        {

        }

        #region 设置面板数据源
        /// <summary>
        /// 功能描述:设置面板数据源
        /// 作  者:HZH
        /// 创建日期:2019-06-25 15:02:15
        /// 任务编号:POS
        /// </summary>
        /// <param name="lstSource">lstSource</param>
        public void SetSource(List<KeyValuePair<string, string>> lstSource)
        {
            try
            {
                ControlHelper.FreezeControl(this, true);
                if (row <= 0 || column <= 0)
                    return;
                if (Source != lstSource)
                    Source = lstSource;
                int index = 0;
                SelectBtn = null;
                foreach (UCBtnExt btn in this.panMain.Controls)
                {
                    if (lstSource != null && index < lstSource.Count)
                    {
                        btn.BtnText = lstSource[index].Value;
                        btn.Tag = lstSource[index].Key;
                        index++;
                    }
                    else
                    {
                        btn.BtnText = "";
                        btn.Tag = null;
                    }
                }
            }
            finally
            {
                ControlHelper.FreezeControl(this, false);
            }
        }
        #endregion
        /// <summary>
        /// 设置选中项
        /// </summary>
        /// <param name="strKey"></param>
        public void SetSelect(string strKey)
        {
            foreach (UCBtnExt item in this.panMain.Controls)
            {
                if (item.Tag != null && item.Tag.ToStringExt() == strKey)
                {
                    SelectBtn = item;
                    return;
                }
            }
            SelectBtn = new UCBtnExt();
        }

        #region 重置面板
        /// <summary>
        /// 功能描述:重置面板
        /// 作  者:HZH
        /// 创建日期:2019-06-25 15:02:05
        /// 任务编号:POS
        /// </summary>
        public void ReloadPanel()
        {
            if (row <= 0 || column <= 0)
                return;
            SelectBtn = null;
            this.panMain.Controls.Clear();
            this.panMain.ColumnCount = column;
            this.panMain.ColumnStyles.Clear();
            for (int i = 0; i < column; i++)
            {
                this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            }

            this.panMain.RowCount = row;
            this.panMain.RowStyles.Clear();
            for (int i = 0; i < row; i++)
            {
                this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            }

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    UCBtnExt btn = new UCBtnExt();
                    btn.BackColor = System.Drawing.Color.Transparent;
                    btn.BtnBackColor = System.Drawing.Color.Transparent;
                    btn.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
                    btn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
                    btn.ConerRadius = 5;
                    btn.Dock = DockStyle.Fill;
                    btn.FillColor = System.Drawing.Color.White;
                    btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
                    btn.Cursor = Cursor.Current;
                    btn.IsShowRect = true;
                    btn.IsRadius = true;
                    btn.IsShowTips = false;
                    btn.Name = "btn_" + i + "_" + j;
                    btn.RectColor = System.Drawing.Color.White;
                    btn.RectWidth = 1;
                    btn.Width = this.Width;
                    btn.TabIndex = 0;
                    btn.TipsText = "";
                    btn.BtnClick += btn_BtnClick;
                    this.panMain.Controls.Add(btn, j, i);
                }
            }

            if (Source != null)
            {
                SetSource(Source);
            }
        }
        #endregion

        void btn_BtnClick(object sender, EventArgs e)
        {
            var btn = (UCBtnExt)sender;
            if (btn.Tag == null)
                return;
            SelectBtn = btn;
        }
    }
}
namespace HZH_Controls.Controls
{
    partial class UCTimePanel
    {
        /// <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.panMain = new System.Windows.Forms.TableLayoutPanel();
            this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
            this.ucSplitLine_V2 = new HZH_Controls.Controls.UCSplitLine_V();
            this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
            this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H();
            this.SuspendLayout();
            // 
            // panMain
            // 
            this.panMain.ColumnCount = 1;
            this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.panMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panMain.Location = new System.Drawing.Point(1, 1);
            this.panMain.Name = "panMain";
            this.panMain.RowCount = 1;
            this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.panMain.Size = new System.Drawing.Size(99, 228);
            this.panMain.TabIndex = 0;
            // 
            // ucSplitLine_V1
            // 
            this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
            this.ucSplitLine_V1.Location = new System.Drawing.Point(0, 0);
            this.ucSplitLine_V1.Name = "ucSplitLine_V1";
            this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 230);
            this.ucSplitLine_V1.TabIndex = 0;
            this.ucSplitLine_V1.TabStop = false;
            this.ucSplitLine_V1.Visible = false;
            // 
            // ucSplitLine_V2
            // 
            this.ucSplitLine_V2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_V2.Dock = System.Windows.Forms.DockStyle.Right;
            this.ucSplitLine_V2.Location = new System.Drawing.Point(100, 0);
            this.ucSplitLine_V2.Name = "ucSplitLine_V2";
            this.ucSplitLine_V2.Size = new System.Drawing.Size(1, 230);
            this.ucSplitLine_V2.TabIndex = 1;
            this.ucSplitLine_V2.TabSto
WinForms中的自定义控件开发允许开发者创建具有特定功能的控件,这些控件可以用于多种应用程序。仪表盘控件是一种常见的自定义控件,它模拟物理仪表盘,用于显示从简单到复杂的各种数据。在C#中开发WinForms仪表盘控件通常涉及以下几个步骤: 1. **创建控件类**:首先,你需要继承自`UserControl`类,创建一个新的类,这个类将作为基础来定义你的仪表盘控件。 2. **设计界面**:在类中,使用设计器或代码来绘制控件的用户界面。这可能包括刻度、指针和数据标签等元素。 3. **编写业务逻辑**:实现控件的数据绑定和逻辑处理,这可能包括如何读取和显示数据,以及如何响应用户的交互。 4. **属性和事件**:定义公共属性来获取和设置控件的外观和行为,如颜色、范围等,并且创建事件以允许外部代码响应特定的用户操作,如值改变等。 5. **测试和调试**:在完成开发后,需要对控件进行彻底的测试,确保在不同情况下都能正确工作。 以下是一个简单的示例代码,展示了如何创建一个基础的仪表盘控件: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class DashboardControl : UserControl { private float value = 0; // 仪表盘的当前值 // 公共属性,允许外部设置仪表盘值 public float Value { get { return value; } set { if (value != this.value) { this.value = value; Invalidate(); // 重绘控件 } } } // 绘制控件 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; // 绘制仪表盘的刻度和指针等 // ... } // 其他方法,如响应用户事件等 } ``` 开发自定义控件是一个复杂的过程,它需要深入了解WinForms框架、GDI+绘图以及可能涉及的动画和数据绑定技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值