DateTimePicker: Highlight today's date only

5 posts, 1 answers
  1. Ian
    Ian avatar
    22 posts
    Member since:
    Nov 2010

    Posted 04 Apr 2012 Link to this post

    Hi,

    Is there a way to highlight today's date only and not the selected date on dateTimePicker's calendar?

    Many Thanks,

    Ian
  2. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 09 Apr 2012 Link to this post

    Hello,

    Thank you for contacting us.

    You should set CallendarCell Focused property to False in the PopupControl_PopupOpened event.
    Please refer to the code below:
    public partial class Form1 : Form
        {
            RadDateTimePickerCalendar calendar;
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                calendar = (RadDateTimePickerCalendar)this.radDateTimePicker1.DateTimePickerElement.CurrentBehavior;
                calendar.PopupControl.PopupOpened += new RadPopupOpenedEventHandler(PopupControl_PopupOpened);
            }
             
            void PopupControl_PopupOpened(object sender, EventArgs args)
            {
                RadElementCollection children = calendar.Calendar.CalendarElement.CalendarVisualElement.Children[0].Children[1].Children;
                for (int i = 0; i < children.Count;++i)
                {
                    CalendarCellElement cell = (CalendarCellElement)children[i];
                    if (cell.Focused)
                    {
                        cell.Focused = false;
                    }
                }
            }
        }

    I hope this helps.

    Kind regards,
    Peter
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ian
    Ian avatar
    22 posts
    Member since:
    Nov 2010

    Posted 25 Apr 2012 Link to this post

    Hi Peter,

    Thank you for your reply. The code is not working but when I added "cell.Selected = false;" after "cell.Focused= false;", it's working now. Thank you for the answer.

    Kind regards,

    Ian
  5. Ian
    Ian avatar
    22 posts
    Member since:
    Nov 2010

    Posted 19 Jul 2012 Link to this post

    Hi,

    I found a bug in this solution. While still on the calender, if you move forward a month and then return back to the current month, both the selected date and date today is highlighted. I tried adding the code in radDateTimePicker1.Calendar.MouseClick event but it's still not working. I hope you can help me with this.

    Many Thanks,

    Ian
  6. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 23 Jul 2012 Link to this post

    Hi Ian,

    Thank you for writing back.

    To handle all similar cases you should handle Paint event - this is not the best practice and we do not recommend it, but is the only possible solution to achieve the desired behavior.

    Please, refer to the code below:
    public partial class Form1 : Form
    {
        RadDateTimePickerCalendar calendar;
        public Form1()
        {
            InitializeComponent();
        }
     
        private void Form1_Load(object sender, EventArgs e)
        {
            calendar = (RadDateTimePickerCalendar)this.radDateTimePicker1.DateTimePickerElement.CurrentBehavior;
            calendar.Calendar.Paint += new PaintEventHandler(Calendar_Paint);
             
        }
     
        void Calendar_Paint(object sender, PaintEventArgs e)
        {
            RadElementCollection children = calendar.Calendar.CalendarElement.CalendarVisualElement.Children[0].Children[1].Children;
            for (int i = 0; i < children.Count; ++i)
            {
                CalendarCellElement cell = (CalendarCellElement)children[i];
                if (cell.Focused)
                {
                    cell.Focused = false;
                    cell.Selected = false;
                }
            }
        }
     
        
    }

    I hope this helps. All the best,
    Peter
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready