protected void dayClick(object sender, System.EventArgs e) { DayLabel l = sender as DayLabel; l.Focus(); DateTime dt = l.Date; selectedDayLabel.BackColor = Color.Transparent; selectedDayLabel.ForeColor = foreColor; if (dt.Month == selectedDate.Month) { selectedDate = dt; selectedDayLabel = l; l.BackColor = selectedDateBackColor; l.ForeColor = selectedDateForeColor; } else { selectedDate = dt; drawCalendar(); } dateTimePicker.Value = selectedDate; if (SelectedDateChanged != null) { SelectedDateChanged(this, new System.EventArgs()); } }
public VisualMonthCalendar() { SetStyle(ControlStyles.FixedHeight, true); SetStyle(ControlStyles.FixedWidth, true); SetStyle(ControlStyles.DoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.CacheText, true); SetStyle(ControlStyles.Selectable, true); InitializeComponent(); // default value for colors, etc. colorLeft = SystemColors.ControlLightLight; colorRight = SystemColors.Control; titleBarColorLeft = Color.WhiteSmoke; titleBarColorRight = Color.Silver; selectedDateForeColor = SystemColors.ActiveCaptionText; selectedDateBackColor = SystemColors.ActiveCaption; foreColor = SystemColors.ControlText; inactiveForeColor = SystemColors.GrayText; dayColor = SystemColors.Highlight; titleBarForeColor = SystemColors.ControlText; sepColorTop = Color.WhiteSmoke; sepColorBottom = Color.Silver; calGradient = LinearGradientMode.ForwardDiagonal; titleGradient = LinearGradientMode.Horizontal; calFont = SystemInformation.MenuFont; dayFont = SystemInformation.MenuFont; selectedDayFont = new Font(SystemInformation.MenuFont, FontStyle.Bold); visualStyle = true; useBaseGradient = false; // The individual days of the month are displayed via a 2-dimensional array of Label controls. for (int w = 0, y = titleBar.Height + 19; w < 6; w++) { for (int d = 0, x = 2; d < 7; d++) { dayLabels[w, d] = new DayLabel(); dayLabels[w, d].BackColor = Color.Transparent; dayLabels[w, d].TextAlign = System.Drawing.ContentAlignment.MiddleCenter; dayLabels[w, d].Size = new Size(28, 16); dayLabels[w, d].Location = new Point(x, y); dayLabels[w, d].Click += new System.EventHandler(dayClick); dayLabels[w, d].TabIndex = 1001 + d + (w * 10); x += dayLabels[w, d].Width; Controls.Add(dayLabels[w, d]); } y += dayLabels[w, 0].Height; } // Initialize day name labels for (int c = 0, xp = 2; c < 7; c++) { dayNameLabels[c] = new System.Windows.Forms.Label(); dayNameLabels[c].BackColor = Color.Transparent; dayNameLabels[c].TextAlign = System.Drawing.ContentAlignment.MiddleCenter; dayNameLabels[c].Size = new Size(28, 15); dayNameLabels[c].Location = new Point(xp, titleBar.Height + 2); Controls.Add(dayNameLabels[c]); xp += dayNameLabels[c].Width; } // default TodayDate and SelectedDate values todayDate = DateTime.Today; SelectedDate = DateTime.Today; dateTimePicker.CustomFormat = Application.CurrentCulture.DateTimeFormat.YearMonthPattern; dateTimePicker.LostFocus += new System.EventHandler(dateTimePicker_LostFocus); dateTimePicker.Leave += new System.EventHandler(dateTimePicker_LostFocus); dateTimePicker.ValueChanged += new System.EventHandler(dateTimePicker_DateChanged); }
protected void drawCalendar() { titleBar.Text = selectedDate.ToString(Application.CurrentCulture.DateTimeFormat.YearMonthPattern); titleBar.ForeColor = titleBarForeColor; titleBar.Font = calFont; buttonLeft.Font = calFont; buttonRight.Font = calFont; DateTime stime = new DateTime(selectedDate.Year, selectedDate.Month, 1, 12, 0, 0, 0); if ((int)stime.DayOfWeek == 0) { stime = stime.Subtract(new TimeSpan(7, 0, 0, 0)); } else { stime = stime.Subtract(new TimeSpan((int)stime.DayOfWeek, 0, 0, 0)); } for (int w = 0; w < 6; w++) { for (int d = 0; d < 7; d++, stime = stime.AddDays(1)) { if (w == 0) // This is necessary to display the day names in the user's language. { dayNameLabels[d].Text = stime.ToString("ddd"); dayNameLabels[d].ForeColor = dayColor; dayNameLabels[d].Font = calFont; } if (stime.Day == selectedDate.Day && stime.Month == selectedDate.Month && stime.Year == selectedDate.Year) { selectedDayLabel = dayLabels[w, d]; dayLabels[w, d].BackColor = selectedDateBackColor; dayLabels[w, d].ForeColor = selectedDateForeColor; } else if (stime.Month != selectedDate.Month) { dayLabels[w, d].ForeColor = inactiveForeColor; dayLabels[w, d].BackColor = Color.Transparent; if (stime.Day >= 28) { lastMonthDate = stime; } } else { dayLabels[w, d].ForeColor = foreColor; dayLabels[w, d].BackColor = Color.Transparent; } dayLabels[w, d].Font = calFont; if (stime.Day == todayDate.Day && stime.Month == todayDate.Month && stime.Year == todayDate.Year && stime.Month == selectedDate.Month) { dayLabels[w, d].Font = selectedDayFont; } else { dayLabels[w, d].Font = dayFont; } dayLabels[w, d].Text = stime.Day.ToString(); dayLabels[w, d].Date = stime; } } }