DateTimePicker changing background colour

3 posts, 0 answers
  1. Mike
    Mike avatar
    2 posts
    Member since:
    Oct 2017

    Posted 02 Oct Link to this post

    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. Dimitar
    Admin
    Dimitar avatar
    1871 posts

    Posted 03 Oct Link to this post

    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.
  3. Mike
    Mike avatar
    2 posts
    Member since:
    Oct 2017

    Posted 03 Oct in reply to Dimitar Link to this post

    Thanks for that. Works perfectly
Back to Top