This is a migrated thread and some comments may be shown as answers.
Restricted date selection in RadTimePicker
1 Answer 71 Views
This is a migrated thread and some comments may be shown as answers.
Jeff Barnd
Top achievements
Rank 1
Jeff Barnd asked on 27 Jan 2009, 06:29 PM
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-

1 Answer, 1 is accepted

Sort by
0
Boyko Markov
Telerik team
answered on 02 Feb 2009, 01:18 PM
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.
Asked by
Jeff Barnd
Top achievements
Rank 1
Answers by
Boyko Markov
Telerik team
Share this question
or