C# 自定义日历控件

效果预览:
显示一周日期:

 

 下拉先展示一月日期

点击"<" 、">"按钮切换月份

具体代码:
form 窗体:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TimeControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            initControl();
        }

        /// <summary>
        /// 自定义控件
        /// </summary>
        private CircleLabelControl selectCircLabel = new CircleLabelControl();

        /// <summary>
        /// 选择日期
        /// </summary>
        private DateTime selectDate = DateTime.Now;

        /// <summary>
        /// 日历显示模式:true显示一周, false显示一个月
        /// </summary>
        private bool isWeekMode = true;

        /// <summary>
        /// 用于存储界面显示的日期集合
        /// </summary>
        private Dictionary<Point , DateTime> timeDic = new Dictionary<Point, DateTime>();

        /// <summary>
        /// 图片位置,根据自己实际更换
        /// </summary>
        private string show = @"G:\emrVS01\TimeControl\TimeControl\Resoutces\展开.jpg";

        /// <summary>
        /// 图片位置,根据自己实际更换
        /// </summary>
        private string fold = @"G:\\emrVS01\\TimeControl\\TimeControl\\Resoutces\\收起.jpg";

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void initControl()
        {
            this.StartPosition = FormStartPosition.CenterParent;
            this.time.SelectionStart = DateTime.Now;
            this.time.FirstDayOfWeek = Day.Sunday;
            this.time.MaxSelectionCount = 7;


            this.lbTimeShow.Text = DateTime.Now.ToString("yyyy | MM.dd");
            this.picMode.Click += pictureBox1_Click;
            this.lbLeft.Click += label3_Click;
            this.lbRight.Click += label4_Click;  
            picMode.ImageLocation = show;
            DateTime dateNow = DateTime.Now;
            DayOfWeek week = dateNow.DayOfWeek;
            setDate(dateNow, week, 0);
            foreach (KeyValuePair<Point, DateTime> point in timeDic)
            {
                if (point.Value == Convert.ToDateTime(selectDate.ToString("yyyy-MM-dd")))
                {
                    foreach (Control control in this.pnlDate.Controls)
                    {
                        if (control.Location == point.Key)
                        {
                            CircleLabelControl circleLabel = (CircleLabelControl)control;
                            setSelectCircLabel(circleLabel);
                            break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 设置日期方法
        /// </summary>
        /// <param name="date">传入时间</param>
        /// <param name="week">传入时间属于星期几</param>
        /// <param name="y">时间y坐标</param>
        private void setDate(DateTime date, DayOfWeek week, int y)
        {
            DateTime dateTimeDic = DateTime.Now;
            for (int i = 0; i <= 7; i++)
            {
                CircleLabelControl circleLabel = new 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值