-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendar.cs
134 lines (127 loc) · 4.84 KB
/
Calendar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 Organizer
{
public partial class Calendar : UserControl
{
public delegate void SelectHandler(DateTime d);
public event SelectHandler? SelectDay;
private DateTime selecteddate = DateTime.Now;
private DateTime date = DateTime.Now;
private String[] months = { "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" };
public Calendar()
{
InitializeComponent();
Month_GridView.RowTemplate.Height = Month_GridView.Height / 6;
for (int i = 0; i < 6; i++)
{
Month_GridView.Rows.Add();
Month_GridView.Rows[i].Height = Month_GridView.Height / 6 - 5;
}
Month_GridView.Height = Month_GridView.RowTemplate.Height * 6;
ChangeMonth();
}
private void ChangeMonth()
{
MonthName_Box.Text = Convert.ToString(date.Year) +" "+ months[date.Month - 1];
int firstdate=-1;
DateTime d = new DateTime(date.Year, date.Month, 1);
switch (d.DayOfWeek)
{
case DayOfWeek.Monday:
firstdate = 0;
break;
case DayOfWeek.Tuesday:
firstdate = 1;
break;
case DayOfWeek.Wednesday:
firstdate = 2;
break;
case DayOfWeek.Thursday:
firstdate = 3;
break;
case DayOfWeek.Friday:
firstdate = 4;
break;
case DayOfWeek.Saturday:
firstdate = 5;
break;
case DayOfWeek.Sunday:
firstdate = 6;
break;
}
Month_GridView.Rows[0].Cells[firstdate].Value = 1;
Month_GridView.Rows[0].Cells[firstdate].Tag = new DateTime(date.Year, date.Month, 1);
for (int i = 0;i<firstdate ;i++)
{
Month_GridView.Rows[0].Cells[firstdate - (i+1)].Value = DateTime.DaysInMonth(date.Year, date.Month == 1 ? 12:date.Month-1)-i;
Month_GridView.Rows[0].Cells[firstdate - (i+1)].Tag = new DateTime(date.Year, date.Month == 1 ? 12 : date.Month - 1, DateTime.DaysInMonth(date.Year, date.Month == 1 ? 12 : date.Month - 1) - i);//исправить
}
for (int i = 1; i < 7-firstdate; i++)
{
Month_GridView.Rows[0].Cells[firstdate + i].Value = i + 1;
Month_GridView.Rows[0].Cells[firstdate + i].Tag = new DateTime(date.Year, date.Month, i + 1);
}
int n = 8 - firstdate;
int m = date.Month;
for (int i = 1; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
d = (m == 13) ? new DateTime(date.Year + 1, 1, n):new DateTime(date.Year,m,n);
Month_GridView.Rows[i].Cells[j].Value = n++;
Month_GridView.Rows[i].Cells[j].Tag = d;
if (n > DateTime.DaysInMonth(date.Year, date.Month)) { n = 1;m++; }
}
}
}
private void Back_Button_Click(object sender, EventArgs e)
{
if (date.Month == 1)
{
date = new DateTime(date.Year - 1, 12, 31);
}
else
{
date = new DateTime(date.Year,date.Month - 1, DateTime.DaysInMonth(date.Year,date.Month-1));
}
ChangeMonth();
}
private void Forward_Button_Click(object sender, EventArgs e)
{
if (date.Month == 12)
{
date = new DateTime(date.Year + 1, 1, 1);
}
else
{
date = new DateTime(date.Year, date.Month + 1, 1);
}
ChangeMonth();
}
private void Month_GridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
selecteddate = (DateTime)Month_GridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag;
SelectDay.Invoke(selecteddate);
}
public DateTime SelectedDate
{
get { return selecteddate; }
}
private void Month_GridView_Resize(object sender, EventArgs e)
{
int h = Month_GridView.Height / 6;
for (int i = 0; i < 6; i++)
{
Month_GridView.Rows[i].Height = h - 4;
}
}
}
}