如何:使用 Windows Form MonthCalendar 控制項以粗體顯示特定日期

Windows Forms 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();  
    

另請參閱