This is a migrated thread and some comments may be shown as answers.
DateTimePicker changing background colour
2 Answers 23 Views
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 02 Oct 2017, 03:35 PM

I have a datetimepicker field where I want to change the background colour of special days.

I have set up event handlers for the clicking of the arrows but what event is fired when the user changes the month drop down on the picker.

My code as it stands is

       public form1()
        {
            InitializeComponent();

            RadDateTimePickerCalendar calendarBehavior = this.dtDOI.DateTimePickerElement.GetCurrentBehavior() as RadDateTimePickerCalendar;
            calendar = calendarBehavior.Calendar as RadCalendar;
            RadCalendarElement calendarElement = calendar.CalendarElement as RadCalendarElement;
            MonthViewElement monthView = calendarBehavior.Calendar.CalendarElement.CalendarVisualElement as MonthViewElement;
            calendarElement.NextButton.Click += new EventHandler(calendar_SelectionChanged);   
            calendarElement.PreviousButton.Click += new EventHandler(calendar_SelectionChanged);
            calendarElement.FastBackwardButton.Click += new EventHandler(calendar_SelectionChanged);
            calendarElement.FastForwardButton.Click += new EventHandler(calendar_SelectionChanged);
         }

        void calendar_SelectionChanged(object sender, EventArgs e)
        {
            FormatCalendar();
        }

        private void FormatCalendar()
        {
            RadDateTimePickerCalendar calendarBehavior = this.dtDOI.DateTimePickerElement.GetCurrentBehavior() as RadDateTimePickerCalendar;
            RadCalendar calendar = calendarBehavior.Calendar as RadCalendar;
            MonthViewElement monthView = calendarBehavior.Calendar.CalendarElement.CalendarVisualElement as MonthViewElement;
            RadCalendarDay[] specialDays = calendar.SpecialDays.ToArray();
            List<DateTime> dates = ExtractSpecialdates(specialDays);

            foreach (CalendarCellElement cell in monthView.TableElement.Children)
            {
                if (dates.Contains(cell.Date))
                {
                    cell.BackColor = Color.Green;
                }
                else
                {
                    cell.BackColor = Color.White;
                }
            }

        }

If I use   calendar.ElementRender += new RenderElementEventHandler(calender_SelectionChanged); I get stuck in a perpetual loop. 

Or is there a better way of doing things?

2 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 03 Oct 2017, 11:21 AM
Hi, Mike,

The following code works fine on my side:
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    List<DateTime> specialDays = new List<DateTime>();
    public RadForm1()
    {
        InitializeComponent();
        var calendar = radDateTimePicker1.DateTimePickerElement.Calendar as RadCalendar;
       
 
        calendar.ElementRender += Calendar_ElementRender;
        specialDays.Add(DateTime.Now.AddDays(2));
        specialDays.Add(DateTime.Now.AddDays(4));
    }
    
    private void Calendar_ElementRender(object sender, RenderElementEventArgs e)
    {
        if (ContainsDay(e.Day.Date.Date))
        {
            e.Element.BackColor = Color.Red;
        }
    }
    public bool ContainsDay(DateTime date)
    {
        foreach (var item in specialDays)
        {
            if (item.Date == date.Date)
            {
                return true;
            }
        }
        return false;
    }
}

I hope this will be useful. Let me know if you have additional questions.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mike
Top achievements
Rank 1
answered on 03 Oct 2017, 12:48 PM
Thanks for that. Works perfectly
Asked by
Mike
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Mike
Top achievements
Rank 1
Share this question
or