|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace JwKdsV.Component
|
|
|
|
|
{
|
|
|
|
|
public partial class DateTimePickerEx2 : Form
|
|
|
|
|
{
|
|
|
|
|
private DateTimeFormatInfo dtfi = new CultureInfo("zh-CN", false).DateTimeFormat;
|
|
|
|
|
//private DateTimeFormatInfo dtfi = new System.Globalization.DateTimeFormatInfo();
|
|
|
|
|
|
|
|
|
|
private int COLUMN_WIDTH = 40;
|
|
|
|
|
|
|
|
|
|
private int COLUMN_HEIGHT = 30;
|
|
|
|
|
|
|
|
|
|
private Color _backColor = Color.White;
|
|
|
|
|
private DateTime _currentDate;
|
|
|
|
|
|
|
|
|
|
public DateTimePicker cmb_Cal;
|
|
|
|
|
|
|
|
|
|
public DateTime CurrentDate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _currentDate;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_currentDate = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTimePickerEx2(Color headerColor)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//每周第一天为星期一
|
|
|
|
|
dtfi.FirstDayOfWeek = DayOfWeek.Monday;
|
|
|
|
|
dtfi.DayNames = new string[] { "一", "二", "三", "四", "五", "六", "日" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
|
|
|
|
|
|
|
|
|
|
dataGridViewCellStyle.BackColor = pn_Controls.BackColor = headerColor;
|
|
|
|
|
dataGridViewCellStyle.Font = new Font("宋体", 12.25F, FontStyle.Regular, GraphicsUnit.Point, (204));
|
|
|
|
|
dgv_Dates.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DateTimePickerEx_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (cmb_Cal.Format == DateTimePickerFormat.Short || cmb_Cal.Format == DateTimePickerFormat.Long)
|
|
|
|
|
{
|
|
|
|
|
pn_TrackBar.Visible = false;
|
|
|
|
|
|
|
|
|
|
this.Width = 7 * COLUMN_WIDTH + 12;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.Width = 7 * COLUMN_WIDTH + 12 + pn_TrackBar.Width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddGridViewColumns();
|
|
|
|
|
|
|
|
|
|
FillCells();
|
|
|
|
|
|
|
|
|
|
FillComboxes();
|
|
|
|
|
|
|
|
|
|
SelectCell(_currentDate.Day, _currentDate.Month);
|
|
|
|
|
|
|
|
|
|
lbl_Today.Text = "今天: " + DateTime.Now.ToString("yyyy-MM-dd");
|
|
|
|
|
|
|
|
|
|
tr_Hrs.NumValue = _currentDate.Hour;
|
|
|
|
|
|
|
|
|
|
tr_Mins.NumValue = _currentDate.Minute;
|
|
|
|
|
|
|
|
|
|
tr_Secs.NumValue = _currentDate.Second;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fills calendar datagridview
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void AddGridViewColumns()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Adds columns with day names
|
|
|
|
|
string[] dayNames = dtfi.DayNames;
|
|
|
|
|
for (int i = 0; i < dayNames.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
dgv_Dates.Columns.Add("col" + i, dayNames[i]);
|
|
|
|
|
|
|
|
|
|
dgv_Dates.Columns[i].Width = COLUMN_WIDTH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sets columns as non sortable
|
|
|
|
|
foreach (DataGridViewColumn column in dgv_Dates.Columns)
|
|
|
|
|
{
|
|
|
|
|
column.SortMode = DataGridViewColumnSortMode.NotSortable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adds empty rows for dates numbers
|
|
|
|
|
string[] dayNumber = new string[dayNames.Count()];
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < dayNames.Count(); x++)
|
|
|
|
|
{
|
|
|
|
|
dayNumber[x] = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dgv_Dates.Rows.Add(dayNumber);
|
|
|
|
|
|
|
|
|
|
dgv_Dates.Rows[i].Height = COLUMN_HEIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void dgvDates_CellEnter(object sender, DataGridViewCellEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(dgv_Dates.CurrentCell.Value.ToString()) && Convert.ToInt32(dgv_Dates.CurrentCell.Value) != _currentDate.Day)
|
|
|
|
|
{
|
|
|
|
|
if (dgv_Dates.CurrentCell.Tag != null)
|
|
|
|
|
{
|
|
|
|
|
var day = Convert.ToInt32(dgv_Dates.CurrentCell.Value);
|
|
|
|
|
var month = Convert.ToInt32(dgv_Dates.CurrentCell.Tag);
|
|
|
|
|
_currentDate = new DateTime(_currentDate.Year, month, day,
|
|
|
|
|
_currentDate.Hour, _currentDate.Minute, _currentDate.Second,
|
|
|
|
|
_currentDate.Millisecond);
|
|
|
|
|
|
|
|
|
|
if (Convert.ToDateTime(cmb_Cal.Value).Month != _currentDate.Month)
|
|
|
|
|
{
|
|
|
|
|
int currentYear = _currentDate.Year;
|
|
|
|
|
if (_currentDate.Month == 12 && Convert.ToDateTime(cmb_Cal.Value).Month == 1) currentYear--;
|
|
|
|
|
if (_currentDate.Month == 1 && Convert.ToDateTime(cmb_Cal.Value).Month == 12) currentYear++;
|
|
|
|
|
|
|
|
|
|
FillCells();
|
|
|
|
|
cmb_Years.SelectedItem = currentYear;
|
|
|
|
|
cmb_Months.SelectedValue = _currentDate.Month;
|
|
|
|
|
dgv_Dates.ClearSelection();
|
|
|
|
|
SelectCell(_currentDate.Day, _currentDate.Month);
|
|
|
|
|
}
|
|
|
|
|
cmb_Cal.Value = _currentDate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private DateTime FirstDayOfMonth(DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
return new DateTime(dateTime.Year, dateTime.Month, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fills calendar DataGridView with date numbers
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void FillCells()
|
|
|
|
|
{
|
|
|
|
|
string[] dayNames = dtfi.DayNames;
|
|
|
|
|
var firstDay = (int)FirstDayOfMonth(_currentDate).DayOfWeek;
|
|
|
|
|
if (firstDay == 0) firstDay = 7;
|
|
|
|
|
DateTime prevMonthDate;
|
|
|
|
|
|
|
|
|
|
if (firstDay == 1)
|
|
|
|
|
{
|
|
|
|
|
prevMonthDate = FirstDayOfMonth(_currentDate).AddDays(-7);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
prevMonthDate = FirstDayOfMonth(_currentDate).AddDays(-(firstDay - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int nrDay = prevMonthDate.Day;
|
|
|
|
|
int nrMonth = prevMonthDate.Month;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < dayNames.Count(); x++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//if current month is reached
|
|
|
|
|
if (nrDay > DateTime.DaysInMonth(_currentDate.Year, _currentDate.AddMonths(-1).Month) && nrMonth == _currentDate.AddMonths(-1).Month)
|
|
|
|
|
{
|
|
|
|
|
nrDay = 1;
|
|
|
|
|
nrMonth = _currentDate.Month;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if next month is reached
|
|
|
|
|
if (nrDay > DateTime.DaysInMonth(_currentDate.Year, _currentDate.Month) && nrMonth == _currentDate.Month)
|
|
|
|
|
{
|
|
|
|
|
nrDay = 1;
|
|
|
|
|
nrMonth = _currentDate.AddMonths(1).Month;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dgv_Dates.Rows[i].Cells[x].Value = nrDay;
|
|
|
|
|
dgv_Dates.Rows[i].Cells[x].Tag = nrMonth;
|
|
|
|
|
|
|
|
|
|
//Changes font style when current month is reached
|
|
|
|
|
if (Convert.ToInt32(dgv_Dates.Rows[i].Cells[x].Tag) != _currentDate.Month)
|
|
|
|
|
{
|
|
|
|
|
dgv_Dates.Rows[i].Cells[x].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
|
|
|
|
dgv_Dates.Rows[i].Cells[x].Style.Font = new Font("宋体", 10.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dgv_Dates.Rows[i].Cells[x].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
|
|
|
|
dgv_Dates.Rows[i].Cells[x].Style.Font = new Font("宋体", 10.25F, FontStyle.Bold, GraphicsUnit.Point, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrDay++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fills years and months comboboxes
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void FillComboxes()
|
|
|
|
|
{
|
|
|
|
|
string[] monthNames = dtfi.MonthNames;
|
|
|
|
|
|
|
|
|
|
var monthList = new DataTable();
|
|
|
|
|
|
|
|
|
|
//Fills months combobox
|
|
|
|
|
monthList.Columns.Add(new DataColumn("Month", typeof(string)));
|
|
|
|
|
monthList.Columns.Add(new DataColumn("Id", typeof(int)));
|
|
|
|
|
for (int i = 0; i < 12; i++)
|
|
|
|
|
{
|
|
|
|
|
monthList.Rows.Add(monthList.NewRow());
|
|
|
|
|
monthList.Rows[i][0] = Convert.ToString(monthNames[i]);
|
|
|
|
|
monthList.Rows[i][1] = i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmb_Months.DataSource = monthList;
|
|
|
|
|
cmb_Months.DisplayMember = "Month";
|
|
|
|
|
cmb_Months.ValueMember = "Id";
|
|
|
|
|
|
|
|
|
|
//Fills years combobox
|
|
|
|
|
for (int i = 0; i < 101; i++)
|
|
|
|
|
{
|
|
|
|
|
cmb_Years.Items.Insert(i, i + 1970);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmb_Years.SelectedItem = _currentDate.Year;
|
|
|
|
|
cmb_Months.SelectedValue = _currentDate.Month;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SelectCell(int dayValue, int MonthValue)
|
|
|
|
|
{
|
|
|
|
|
for (int r = 0; r < dgv_Dates.Rows.Count; r++)
|
|
|
|
|
{
|
|
|
|
|
for (int c = 0; c < dgv_Dates.Columns.Count; c++)
|
|
|
|
|
{
|
|
|
|
|
if (Convert.ToInt32(dgv_Dates.Rows[r].Cells[c].Value) == dayValue &&
|
|
|
|
|
Convert.ToInt32(dgv_Dates.Rows[r].Cells[c].Tag) == MonthValue)
|
|
|
|
|
{
|
|
|
|
|
dgv_Dates.BeginInvoke(new ChangeCurrentCellDelegate(ChangeCurrentCell), new object[] { r, c });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private delegate void ChangeCurrentCellDelegate(int columnIndex, int rowIndex);
|
|
|
|
|
private void ChangeCurrentCell(int columnIndex, int rowIndex)
|
|
|
|
|
{
|
|
|
|
|
dgv_Dates.Rows[columnIndex].Cells[rowIndex].Selected = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void DateTimePickerEx_Paint(object sender, PaintEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
BackColor = _backColor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cmb_Years_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (cmb_Years.SelectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
_currentDate = new DateTime((int)cmb_Years.SelectedItem, _currentDate.Month, _currentDate.Day,
|
|
|
|
|
_currentDate.Hour, _currentDate.Minute, _currentDate.Second, _currentDate.Millisecond);
|
|
|
|
|
FillCells();
|
|
|
|
|
|
|
|
|
|
cmb_Cal.Value = _currentDate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cmb_Months_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (cmb_Months.SelectedValue is int)
|
|
|
|
|
{
|
|
|
|
|
int day = _currentDate.Day;
|
|
|
|
|
if (_currentDate.Day > DateTime.DaysInMonth(_currentDate.Year, (int)cmb_Months.SelectedValue)) day = 1;
|
|
|
|
|
_currentDate = new DateTime(_currentDate.Year, (int)cmb_Months.SelectedValue, day,
|
|
|
|
|
_currentDate.Hour, _currentDate.Minute, _currentDate.Second, _currentDate.Millisecond);
|
|
|
|
|
FillCells();
|
|
|
|
|
cmb_Cal.Value = _currentDate;
|
|
|
|
|
SelectCell(1, (int)cmb_Months.SelectedValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void lbl_Today_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_currentDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
FillCells();
|
|
|
|
|
cmb_Years.SelectedItem = _currentDate.Year;
|
|
|
|
|
cmb_Months.SelectedValue = _currentDate.Month;
|
|
|
|
|
cmb_Cal.Value = DateTime.Now;
|
|
|
|
|
SelectCell(DateTime.Now.Day, (int)cmb_Months.SelectedValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets month number for combobox
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void NextPrevMonth(int monthNumber)
|
|
|
|
|
{
|
|
|
|
|
_currentDate = _currentDate.AddMonths(monthNumber);
|
|
|
|
|
FillCells();
|
|
|
|
|
|
|
|
|
|
cmb_Years.SelectedItem = _currentDate.Year;
|
|
|
|
|
cmb_Months.SelectedValue = _currentDate.Month;
|
|
|
|
|
|
|
|
|
|
cmb_Cal.Value = _currentDate;
|
|
|
|
|
SelectCell(1, (int)cmb_Months.SelectedValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void btn_PrevMonth_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
NextPrevMonth(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void btn_NextMonth_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
NextPrevMonth(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnHourNumChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
int hour = (int)tr_Hrs.NumValue;
|
|
|
|
|
|
|
|
|
|
_currentDate = new DateTime(_currentDate.Year, _currentDate.Month, _currentDate.Day,
|
|
|
|
|
hour, _currentDate.Minute, _currentDate.Second, _currentDate.Millisecond);
|
|
|
|
|
|
|
|
|
|
cmb_Cal.Value = _currentDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMinsNumChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
int min = (int)tr_Mins.NumValue;
|
|
|
|
|
|
|
|
|
|
_currentDate = new DateTime(_currentDate.Year, _currentDate.Month, _currentDate.Day,
|
|
|
|
|
_currentDate.Hour, min, _currentDate.Second, _currentDate.Millisecond);
|
|
|
|
|
|
|
|
|
|
cmb_Cal.Value = _currentDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSecChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
int sec = (int)tr_Secs.NumValue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_currentDate = new DateTime(_currentDate.Year, _currentDate.Month, _currentDate.Day,
|
|
|
|
|
_currentDate.Hour, _currentDate.Minute, sec, _currentDate.Millisecond);
|
|
|
|
|
|
|
|
|
|
cmb_Cal.Value = _currentDate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|