如何:使用 Windows 窗体 MonthCalendar 控件以粗体显示特定日期

Windows 窗体 MonthCalendar 控件可以以粗体形式显示天数,可以是单一日期,也可以是重复日期。 你可以这样做来吸引人们对特殊日期的注意,例如假日和周末。

三个属性控制此功能。 BoldedDates 属性包含单个日期。 AnnuallyBoldedDates 属性包含每年都会以粗体显示的日期。 MonthlyBoldedDates 属性包含每月以粗体显示的日期。 其中每个属性都包含 DateTime 对象的数组。 若要从其中一个列表中添加或删除日期,必须添加或删除 DateTime 对象。

使日期以粗体显示

  1. 创建 DateTime 对象。

    Dim myVacation1 As Date = New DateTime(2001, 6, 10)  
    Dim myVacation2 As Date = New DateTime(2001, 6, 17)  
    
    DateTime myVacation1 = new DateTime(2001, 6, 10);  
    DateTime myVacation2 = new DateTime(2001, 6, 17);  
    
    DateTime myVacation1 = DateTime(2001, 6, 10);  
    DateTime myVacation2 = DateTime(2001, 6, 17);  
    
  2. 通过调用 MonthCalendar 控件的 AddBoldedDateAddAnnuallyBoldedDateAddMonthlyBoldedDate 方法,使单个日期加粗。

    MonthCalendar1.AddBoldedDate(myVacation1)  
    MonthCalendar1.AddBoldedDate(myVacation2)  
    
    monthCalendar1.AddBoldedDate(myVacation1);  
    monthCalendar1.AddBoldedDate(myVacation2);  
    
    monthCalendar1->AddBoldedDate(myVacation1);  
    monthCalendar1->AddBoldedDate(myVacation2);  
    

    –或–

    通过创建 DateTime 对象的数组并将其分配给其中一个属性,使一组日期一次性加粗。

    Dim VacationDates As DateTime() = {myVacation1, myVacation2}  
    MonthCalendar1.BoldedDates = VacationDates  
    
    DateTime[] VacationDates = {myVacation1, myVacation2};  
    monthCalendar1.BoldedDates = VacationDates;  
    
    Array<DateTime>^ VacationDates = {myVacation1, myVacation2};  
    monthCalendar1->BoldedDates = VacationDates;  
    

使日期以常规字体显示

  1. 通过调用 RemoveBoldedDateRemoveAnnuallyBoldedDateRemoveMonthlyBoldedDate 方法,使单个加粗日期显示在常规字体中。

    MonthCalendar1.RemoveBoldedDate(myVacation1)  
    MonthCalendar1.RemoveBoldedDate(myVacation2)  
    
    monthCalendar1.RemoveBoldedDate(myVacation1);  
    monthCalendar1.RemoveBoldedDate(myVacation2);  
    
    monthCalendar1->RemoveBoldedDate(myVacation1);  
    monthCalendar1->RemoveBoldedDate(myVacation2);  
    

    –或–

    通过调用 RemoveAllBoldedDatesRemoveAllAnnuallyBoldedDatesRemoveAllMonthlyBoldedDates 方法,从三个列表中删除所有加粗日期。

    MonthCalendar1.RemoveAllBoldedDates()  
    
    monthCalendar1.RemoveAllBoldedDates();  
    
    monthCalendar1->RemoveAllBoldedDates();  
    
  2. 通过调用 UpdateBoldedDates 方法更新字体的外观。

    MonthCalendar1.UpdateBoldedDates()  
    
    monthCalendar1.UpdateBoldedDates();  
    
    monthCalendar1->UpdateBoldedDates();  
    

另请参阅