/// <summary> /// Raises the <see cref="System.Windows.Forms.Control.KeyPress"/> event. /// </summary> /// <param name="e">A <see cref="KeyPressEventArgs"/> that contains the event data.</param> protected override void OnKeyPress(KeyPressEventArgs e) { e.Handled = true; if (!char.IsDigit(e.KeyChar)) { return; } if (!this.inEditMode) { this.inEditMode = true; this.Refresh(); var keyCharString = e.KeyChar.ToString(CultureInfo.InvariantCulture); if (this.datePicker.UseNativeDigits) { var number = int.Parse(keyCharString); keyCharString = DateMethods.GetNativeNumberString(number, this.datePicker.Culture.NumberFormat.NativeDigits, false); } this.inputBox.Font = this.Font; this.inputBox.Location = new Point(0, 2); this.inputBox.Size = this.Size; this.inputBox.Text = keyCharString; this.inputBox.Visible = true; this.inputBox.SelectionStart = 1; this.inputBox.SelectionLength = 0; this.inputBox.BringToFront(); this.inputBox.Focus(); } this.Refresh(); }
/// <summary> /// Draws a single week header element. /// </summary> /// <param name="g">The <see cref="Graphics"/> object used to draw.</param> /// <param name="week">The <see cref="MonthCalendarWeek"/> to draw.</param> public override void DrawWeekHeaderItem(Graphics g, MonthCalendarWeek week) { if (!CheckParams(g, week.Bounds)) { return; } var weekString = this.monthCal.UseNativeDigits ? DateMethods.GetNativeNumberString(week.WeekNumber, this.monthCal.Culture.NumberFormat.NativeDigits, false) : week.WeekNumber.ToString(CultureInfo.CurrentUICulture); // draw week header element using (StringFormat format = GetStringAlignment(this.monthCal.DayTextAlignment)) { // set alignment format.Alignment = StringAlignment.Center; // draw string using (SolidBrush brush = new SolidBrush(this.ColorTable.WeekHeaderText)) { if (this.monthCal.Enabled) { g.DrawString( weekString, this.monthCal.Font, brush, week.Bounds, format); } else { ControlPaint.DrawStringDisabled( g, weekString, this.monthCal.Font, Color.Transparent, week.Bounds, format); } } } }
/// <summary> /// Raises the <see cref="Control.KeyPress"/> event. /// </summary> /// <param name="e">A <see cref="KeyPressEventArgs"/> that contains the event data.</param> protected override void OnKeyPress(KeyPressEventArgs e) { var isNumber = char.IsNumber(e.KeyChar); var isSeparator = this.parent.datePicker.FormatProvider.DateSeparator.Contains(e.KeyChar); var textContainsSeparator = this.Text.Contains(this.parent.datePicker.FormatProvider.DateSeparator); var txtLength = textContainsSeparator ? 10 : 8; if (isSeparator) { if (this.Text.Length == txtLength && e.KeyChar != '\b') { e.Handled = true; } else { base.OnKeyPress(e); } return; } if ((!isNumber || this.Text.Length == txtLength) && e.KeyChar != '\b') { e.Handled = true; return; } if (this.parent.datePicker.UseNativeDigits && isNumber) { var number = int.Parse(e.KeyChar.ToString(CultureInfo.InvariantCulture)); var nativeNumber = DateMethods.GetNativeNumberString(number, this.parent.datePicker.Culture.NumberFormat.NativeDigits, false); e.KeyChar = nativeNumber[0]; } base.OnKeyPress(e); }
/// <summary> /// Draws the header of a <see cref="MonthCalendarMonth"/>. /// </summary> /// <param name="g">The <see cref="Graphics"/> object used to draw.</param> /// <param name="calMonth">The <see cref="MonthCalendarMonth"/> to draw the header for.</param> /// <param name="state">The <see cref="MonthCalendarHeaderState"/>.</param> public override void DrawMonthHeader( Graphics g, MonthCalendarMonth calMonth, MonthCalendarHeaderState state) { if (calMonth == null || !CheckParams(g, calMonth.TitleBounds)) { return; } // get title bounds Rectangle rect = calMonth.TitleBounds; MonthCalendarDate date = new MonthCalendarDate(monthCal.CultureCalendar, calMonth.Date); MonthCalendarDate firstVisible = new MonthCalendarDate(monthCal.CultureCalendar, calMonth.FirstVisibleDate); string month; int year; // gets the month name for the month the MonthCalendarMonth represents and the year string if (firstVisible.Era != date.Era) { month = this.monthCal.FormatProvider.GetMonthName(firstVisible.Year, firstVisible.Month); year = firstVisible.Year; } else { month = this.monthCal.FormatProvider.GetMonthName(date.Year, date.Month); year = date.Year; } string yearString = this.monthCal.UseNativeDigits ? DateMethods.GetNativeNumberString(year, this.monthCal.Culture.NumberFormat.NativeDigits, false) : year.ToString(CultureInfo.CurrentUICulture); // get used font Font headerFont = this.monthCal.HeaderFont; // create bold font Font boldFont = new Font(headerFont.FontFamily, headerFont.SizeInPoints, FontStyle.Bold); // measure sizes SizeF monthSize = g.MeasureString(month, boldFont); SizeF yearSize = g.MeasureString(yearString, boldFont); float maxHeight = Math.Max(monthSize.Height, yearSize.Height); // calculates the width and the starting position of the arrows int width = (int)monthSize.Width + (int)yearSize.Width + 7; int arrowLeftX = rect.X + 6; int arrowRightX = rect.Right - 6; int arrowY = rect.Y + (rect.Height / 2) - 4; int x = Math.Max(0, rect.X + (rect.Width / 2) + 1 - (width / 2)); int y = Math.Max( 0, rect.Y + (rect.Height / 2) + 1 - (((int)maxHeight + 1) / 2)); // set the title month name bounds calMonth.TitleMonthBounds = new Rectangle( x, y, (int)monthSize.Width + 1, (int)maxHeight + 1); // set the title year bounds calMonth.TitleYearBounds = new Rectangle( x + calMonth.TitleMonthBounds.Width + 7, y, (int)yearSize.Width + 1, (int)maxHeight + 1); // generate points for the left and right arrow Point[] arrowLeft = new[] { new Point(arrowLeftX, arrowY + 4), new Point(arrowLeftX + 4, arrowY), new Point(arrowLeftX + 4, arrowY + 8), new Point(arrowLeftX, arrowY + 4) }; Point[] arrowRight = new[] { new Point(arrowRightX, arrowY + 4), new Point(arrowRightX - 4, arrowY), new Point(arrowRightX - 4, arrowY + 8), new Point(arrowRightX, arrowY + 4) }; // get color table MonthCalendarColorTable colorTable = this.ColorTable; // get brushes for normal, mouse over and selected state using (SolidBrush brush = new SolidBrush(colorTable.HeaderText), brushOver = new SolidBrush(colorTable.HeaderActiveText), brushSelected = new SolidBrush(colorTable.HeaderSelectedText)) { // get title month name and year bounds Rectangle monthRect = calMonth.TitleMonthBounds; Rectangle yearRect = calMonth.TitleYearBounds; // set used fonts Font monthFont = headerFont; Font yearFont = headerFont; // set used brushes SolidBrush monthBrush = brush, yearBrush = brush; // adjust brush and font if year selected if (state == MonthCalendarHeaderState.YearSelected) { yearBrush = brushSelected; yearFont = boldFont; yearRect.Width += 4; } else if (state == MonthCalendarHeaderState.YearActive) { // adjust brush if mouse over year yearBrush = brushOver; } // adjust brush and font if month name is selected if (state == MonthCalendarHeaderState.MonthNameSelected) { monthBrush = brushSelected; monthFont = boldFont; monthRect.Width += 4; } else if (state == MonthCalendarHeaderState.MonthNameActive) { // adjust brush if mouse over month name monthBrush = brushOver; } // draws the month name and year string g.DrawString(month, monthFont, monthBrush, monthRect); g.DrawString(yearString, yearFont, yearBrush, yearRect); } boldFont.Dispose(); // if left arrow has to be drawn //if (calMonth.DrawLeftButton) //{ // // get arrow color // Color arrowColor = this.monthCal.LeftButtonState == ButtonState.Normal ? // GetGrayColor(this.monthCal.Enabled, colorTable.HeaderArrow) : colorTable.HeaderActiveArrow; // // set left arrow rect // this.monthCal.SetLeftArrowRect(new Rectangle(rect.X, rect.Y, 15, rect.Height)); // // draw left arrow // using (GraphicsPath path = new GraphicsPath()) // { // path.AddLines(arrowLeft); // using (SolidBrush brush = new SolidBrush(arrowColor)) // { // g.FillPath(brush, path); // } // using (Pen p = new Pen(arrowColor)) // { // g.DrawPath(p, path); // } // } //} // if right arrow has to be drawn // if (calMonth.DrawRightButton) // { // // get arrow color // Color arrowColor = this.monthCal.RightButtonState == ButtonState.Normal ? // GetGrayColor(this.monthCal.Enabled, colorTable.HeaderArrow) : colorTable.HeaderActiveArrow; // // set right arrow rect // this.monthCal.SetRightArrowRect(new Rectangle(rect.Right - 15, rect.Y, 15, rect.Height)); // // draw arrow // using (GraphicsPath path = new GraphicsPath()) // { // path.AddLines(arrowRight); // using (SolidBrush brush = new SolidBrush(arrowColor)) // { // g.FillPath(brush, path); // } // using (Pen p = new Pen(arrowColor)) // { // g.DrawPath(p, path); // } // } // } }
/// <summary> /// Draws a day in the month body of the calendar control. /// </summary> /// <param name="g">The <see cref="Graphics"/> object used to draw.</param> /// <param name="day">The <see cref="MonthCalendarDay"/> to draw.</param> public override void DrawDay(Graphics g, MonthCalendarDay day) { if (!CheckParams(g, day.Bounds)) { return; } // get color table MonthCalendarColorTable colors = this.ColorTable; // get the bounds of the day Rectangle rect = day.Bounds; var boldDate = this.monthCal.BoldedDatesCollection.Find(d => d.Value.Date == day.Date.Date); // if day is selected or in mouse over state if (day.MouseOver) { FillBackground( g, rect, colors.DayActiveGradientBegin, colors.DayActiveGradientEnd, colors.DayActiveGradientMode); } else if (day.Selected) { this.FillBackgroundInternal( g, rect, colors.DaySelectedGradientBegin, colors.DaySelectedGradientEnd, colors.DaySelectedGradientMode); } else if (!boldDate.IsEmpty && boldDate.Category.BackColorStart != Color.Empty && boldDate.Category.BackColorStart != Color.Transparent) { FillBackground( g, rect, boldDate.Category.BackColorStart, boldDate.Category.BackColorEnd.IsEmpty || boldDate.Category.BackColorEnd == Color.Transparent ? boldDate.Category.BackColorStart : boldDate.Category.BackColorEnd, boldDate.Category.GradientMode); } // get bolded dates List <DateTime> boldedDates = this.monthCal.GetBoldedDates(); bool bold = boldedDates.Contains(day.Date) || !boldDate.IsEmpty; // draw the day using (StringFormat format = GetStringAlignment(this.monthCal.DayTextAlignment)) { Color textColor = bold ? (boldDate.IsEmpty || boldDate.Category.ForeColor == Color.Empty || boldDate.Category.ForeColor == Color.Transparent ? colors.DayTextBold : boldDate.Category.ForeColor) : (day.Selected ? colors.DaySelectedText : (day.MouseOver ? colors.DayActiveText : (day.TrailingDate ? colors.DayTrailingText : colors.DayText))); using (SolidBrush brush = new SolidBrush(textColor)) { using (Font font = new Font( this.monthCal.Font.FontFamily, this.monthCal.Font.SizeInPoints, FontStyle.Bold)) { // adjust width Rectangle textRect = day.Bounds; textRect.Width -= 2; // determine if to use bold font //bool useBoldFont = day.Date == DateTime.Today || bold; bool useBoldFont = bold; var calDate = new MonthCalendarDate(monthCal.CultureCalendar, day.Date); string dayString = this.monthCal.UseNativeDigits ? DateMethods.GetNativeNumberString(calDate.Day, this.monthCal.Culture.NumberFormat.NativeDigits, false) : calDate.Day.ToString(this.monthCal.Culture); //if (!day.TrailingDate) //{ if (this.monthCal.Enabled) { g.DrawString( dayString, (useBoldFont ? font : this.monthCal.Font), brush, textRect, format); } else { ControlPaint.DrawStringDisabled( g, dayString, (useBoldFont ? font : this.monthCal.Font), Color.Transparent, textRect, format); } //} } } } // if today, draw border //if (day.Date == DateTime.Today) //{ // rect.Height -= 1; // rect.Width -= 2; // Color borderColor = day.Selected ? colors.DaySelectedTodayCircleBorder // : (day.MouseOver ? colors.DayActiveTodayCircleBorder : colors.DayTodayCircleBorder); // using (Pen p = new Pen(borderColor)) // { // g.DrawRectangle(p, rect); // rect.Offset(1, 0); // g.DrawRectangle(p, rect); // } //} }