Add Calendar - WinForms Calendar Controls
The Calendar Control supports not only all features of the standard Month Calendar control in the Visual Studio toolbox, like first day of week, special days, show today, show week numbers, and hide week numbers, but also additional features that the standard Calendar control do not support. In this tutorial of creating calendar in C# for Windows Forms Applications, we will show how to add selected and special days to the calendar at design time, then iterating and displaying those dates at runtime.
How to add selected and special days to the calendar at design time
- Create a new form in your Windows Forms Application
- Drag and drop a Calendar control to the form.
- Set the property value of EnableMultipleSelect to true.
- Drag and drop a List Control and Button to the form in your WinForms Application.
- Click the ellipses for the SelectedDates property of the Calendar in the Properties Window.
- Click the Add button to append a new selected date to the collection.
- In the Value property for the date, choose today from the drop down calendar.
- Add two more dates. Set the property of Value to the two days following today.
- Click the ellipses for the Special Days property of the Calendar in the Properties Window.
- Click the Add button to append a new Calendar Day to the collection.
- Configure the property of Date for the special day to the last day of the month.
- Set the property of Disabled to true, and set the property of Recurring to DayInMonth.
- Double click the Button item in the designer, and add the below C# code to the Click event handler.
- The C# code will iterate and list results for the SelectedDates and SpecialDays collections.
The following C# code illustrate how to iterate the SelectedDates and SpecialDates collections:
private void button1_Click(object sender, EventArgs e)
foreach (DateTime dateTime in Calendar1.SelectedDates)
new listDataItem("Selected: " + dateTime.ToShortDateString()));
foreach (calendarDay day in Calendar1.SpecialDays)
new listDataItem("Special: " + day.Date.ToShortDateString()));