Restricted date selection in RadTimePicker

2 posts, 0 answers
  1. Jeff Barnd
    Jeff Barnd avatar
    1 posts
    Member since:
    Oct 2008

    Posted 27 Jan 2009 Link to this post

    Hello,
    I'm working with the RadCalendar control in both the Winform environment.  We need to restrict the dates that can be selected in the RadTimePicker control to a specific list of dates.  We also need to highlight the dates that can be selected.  A different background color on these dates would help the users pick a allowed date.  We also need to restrict the user from selecting dates that are not on the allowed list of dates.

    Is there a way to do this?

    Thank you in advance for the feedback and guidance!

    Your help is much appreciated,
    Jeff-
  2. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 02 Feb 2009 Link to this post

    Hi Jeff Barnd,

    I have a solution for the scenario you have. Here it is:

    1. Create a list with the dates you can select.
      List<DateTime> dates = new List<DateTime>();
    2. Subscribe to SelectionChanging, ElementRender and MouseUp of RadCalendar
    3. In the selection changing handler I restrict the selection doing the following:
     void radCalendar1_SelectionChanging(object sender, SelectionEventArgs e)
            {
                Point pt = this.radCalendar1.PointToClient(Cursor.Position);
                foreach (CalendarCellElement cell in (this.radCalendar1.CalendarElement.CalendarVisualElement as MonthViewElement).TableElement.Children)
                {
                    if (cell.HitTest(pt))
                    {
                        if (!this.dates.Contains(cell.Date))
                        {
                            e.Cancel = true;
                            this.radCalendar1.InvalidateCalendar();
                        }
                        break;
                    }
                }
            }
    4. In the ElementRender handler I color the cells which I can select.
     void radCalendar1_ElementRender(object sender, RenderElementEventArgs e)
            {
                if (this.dates.Contains(e.Day.Date))
                {
                    e.Element.DrawFill = true;
                    e.Element.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                    e.Element.BackColor = Color.Red;
                }
                else
                    e.Element.DrawFill = false;
            }
    5. In the MouseUp handler I clear the visual appearance of the focused and selected cells
     Point pt = this.radCalendar1.PointToClient(Cursor.Position);

                foreach (CalendarCellElement cell in (this.radCalendar1.CalendarElement.CalendarVisualElement as MonthViewElement).TableElement.Children)
                {
                    if (cell.HitTest(pt))
                    {
                        if (!this.dates.Contains(cell.Date))
                        {
                            cell.Focused = false;
                            cell.Selected = false;
                        }
                        break;
                    }
                }


    I hope this helps,

    Best wishes,
    Boyko Markov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top