如何使用 C# 在 MonthCalender 中突出显示日期?

How to highlight days in MonthCalender using C#?

我正在使用 visual studio c# 创建一个 window 表单。 window 表单包含月历。

我打算 highlight/bold 我的日历中只有周六和周日(周末)。 我该怎么做呢?因为从月历属性中我只能看到日期而不是天数。

你可以这样实现:

public partial class Form1 : Form
{

 public Form1()
 {
    InitializeComponent();

    var weekends = GetDaysBetween(DateTime.Today.AddMonths(-1), DateTime.Today.AddMonths(12))
.Where(d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday).ToArray();

    monthCalendar1.RemoveAllBoldedDates();
    monthCalendar1.BoldedDates = weekends;
 }
IEnumerable<DateTime> GetDaysBetween(DateTime start, DateTime end)
 {
    for (DateTime i = start; i <= end; i = i.AddDays(1))
    {
        yield return i;
    }
 }

}