效果预览:
显示一周日期:
下拉先展示一月日期
点击"<" 、">"按钮切换月份
具体代码:
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