Calendar for WinForms | ComponentOne
DateEdit / Behavior
In This Topic
    Behavior
    In This Topic

    The C1DateEdit class offers several properties that determines the behavior of the DateEdit control. Users can add a DateEdit control to a form, set a specific date or time, set minimum and maximum dates, disable dates, and show or hide Today/Clear buttons in the calendar dropdown.

    Let's explore the DateEdit behavior in detail.

    Set specific date and time

    Let's say, you want to display a specific date to the end-user in the DateEdit. For this purpose, the control allows you to set a particular date using the Value property in the control.

    DateEdit with calendar UI showing specific date

    The code snippet below shows how to set a specific date:

    C#
    Copy Code
    // Set the Value property to a date value.
    dateEdit.Value = new DateTime(2021, 03, 03);
    

    Let's say, you want to display a specific time with the date in the control. For this purpose, you can again use the Value property.

    C#
    Copy Code
    // Set the Value property to a date and time value
    dateEdit.Value = new DateTime(2021, 03, 15, 10, 0, 0);
    

    Set date range

    DateEdit lets you to customize the dropdown calendar to show a limited set of days in the visible calendar month and restrict the selection to the specified range of dates from the calendar.

    You can specify minimum and maximum dates to prevent users from selecting a date and time in a particular range. Setting a minimum date helps you to restrict selecting the date value that is set less than the specific value. Likewise, setting a maximum date helps you to restrict selecting the date value that is set greater than the specific value.

    In the image below, the minimum and maximum date has been set (to 01/03/2021 and 25/03/2021 respectively). As you can observe, all other date values outside this range are disabled, and only a particular date range can be selected.

    DateEdit with calendar ui showing minimum and maximum range of dates

    Users can set the minimum and maximum date values in the calendar dropdown of DateEdit control, by using the MinDate and MaxDate properties.

    C#
    Copy Code
    // Set a minimum date
    dateEdit.Calendar.MinDate = new DateTime(2021, 3, 1);
    // Set a maximum date
    dateEdit.Calendar.MaxDate = new DateTime(2021, 3, 25);
    

    Disable dates

    Consider a scenario, where you want to disable certain dates in the calendar dropdown. The DateEdit control gives a choice to the users to disable certain dates by using the DisabledDates property.

    In the image below, certain dates have been disabled in the control, such as 03/03/2021, 05/03/2021, 10/03/2021, 15/03/2021, 16/03/2021 and 17/03/2021.

    DateEdit with calendar UI with disabled dates

    The following code shows how to disable dates:

    C#
    Copy Code
    // Set array of dates which need to be disabled
     DateTime[] dateTimes = new DateTime[]
    {
        new DateTime(2021, 03, 03),
        new DateTime(2021, 03, 05),
        new DateTime(2021, 03, 10),
        new DateTime(2021, 03, 15),
        new DateTime(2021, 03, 16),
        new DateTime(2021, 03, 17),
        // etc
    };
    // Add the DisabledDates property to C1DateEditcontrol to disable certain dates
     dateEdit.Calendar.DisabledDates = dateTimes;
    

    Show or hide Today and Clear buttons

    By default, DateEdit comes with a Today button and Clear button functionality. The Today button shows the current date to the end-user, while the Clear button clears the value in the DateEdit editor. The control lets you show or hide Today and Clear buttons in the calendar dropdown of DateEdit using the ShowToday and ShowClearButton properties.

    The following code shows how to hide Today and Clear buttons:

    C#
    Copy Code
    // Hide Today button
    dateEdit.Calendar.ShowToday = false;
    // Hide Clear button
    dateEdit.Calendar.ShowClearButton = false;