This is a migrated thread and some comments may be shown as answers.

Calendar - ENTIRE WEEK selection

3 Answers 1188 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
Speedy
Top achievements
Rank 1
Speedy asked on 03 Aug 2016, 03:48 PM

I am using the 2016 Q2 release of the RadCalendar control...

 

I have reviewed the years old threads regarding the week selection in a Calendar control and I am still unable to achieve what I am after.  Hopefully someone can help...

 

When the form is loaded, I want the Calendar to display the month for the system date, and select all days in the associated week for the system date, using a Sunday - Saturday week format. So if today is Aug 3, 2016, I would want the Calendar to display 7 columns, 6 rows, showing dates Jul 31, 2016 to Sep 10, 2016, with the dates for the current week, Jul 31, 2016 to Aug 6, 2016 selected.

 

Additionally, if the User selects any other date in the Calendar, I want to clear to previous week selection, and select all days in the associated week for the select date, using a Sunday - Saturday week format. So if User selects Aug 24, 2016, I would want the previous week to not be selected, and select all days for which the selected date falls between, Aug 21, 2016 - Aug 27, 2016.

 

In the end, there would only ever be 7 days selected in the Calendar which would equal 1 week in time.

 

Any guidance would be most appreciated.

 

 

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Aug 2016, 06:37 AM
Hello James,

Thank you for writing. 

In order to achieve week selection, you can clear all dates that don't belong to the week from the SelectedDates collection and select each date of the week. Here is demonstrates a sample code snippet which result is illustrated the attached gif file:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.radCalendar1.AllowMultipleSelect = true;
        this.radCalendar1.SelectionChanged += radCalendar1_SelectionChanged;
    }
 
    private void radCalendar1_SelectionChanged(object sender, EventArgs e)
    {
        this.radCalendar1.SelectionChanged -= radCalendar1_SelectionChanged;
        if (this.radCalendar1.SelectedDates.Count != 1)
        {
            while (this.radCalendar1.SelectedDates.Count > 1)
            {
                this.radCalendar1.SelectedDates.Remove(this.radCalendar1.SelectedDates.Last());
            }
        }
        DateTime startDate = this.radCalendar1.SelectedDates.First().StartOfWeek(DayOfWeek.Monday).AddDays(-1);
        DateTime endDate = startDate.AddDays(6);
        while (startDate <= endDate)
        {
            this.radCalendar1.SelectedDates.Add(startDate);
            startDate = startDate.AddDays(1);
        }
   
        this.radCalendar1.SelectionChanged += radCalendar1_SelectionChanged;
    }
}
 
public static class DateTimeExtensions
{
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }
        return dt.AddDays(-1 * diff).Date;
    }
}

Note that this is just a sample code snippet and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 20 Apr 2021, 10:13 AM
the selection  is'nt working when month is change .....can you please check this ?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Apr 2021, 11:13 AM

Hello, Ras Ran,

I was able to observe the described problem when changing the month. I have modified the suggested sample solution. The achieved result is demonstrated in the gif file:

    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            this.radCalendar1.AllowMultipleSelect = true;
            this.radCalendar1.SelectionChanged += radCalendar1_SelectionChanged;
        }
 
        private void radCalendar1_SelectionChanged(object sender, EventArgs e)
        {
            this.radCalendar1.SelectionChanged -= radCalendar1_SelectionChanged;
            if (this.radCalendar1.SelectedDates.Count != 1)
            {
                this.radCalendar1.SelectedDates.Clear();
            }
            DateTime startDate = radCalendar1.FocusedDate.StartOfWeek(DayOfWeek.Monday).AddDays(-1);
            DateTime endDate = startDate.AddDays(6);
            while (startDate <= endDate)
            {
                this.radCalendar1.SelectedDates.Add(startDate);
                startDate = startDate.AddDays(1);
            }
   
            this.radCalendar1.SelectionChanged += radCalendar1_SelectionChanged;
        }
    }
 
    public static class DateTimeExtensions
    {
        public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = dt.DayOfWeek - startOfWeek;
            if (diff < 0)
            {
                diff += 7;
            }
            return dt.AddDays(-1 * diff).Date;
        }
    }

Please give this approach a try and see how it works in your scenario. However, please have in mind that this is just a sample solution and it may not cover all possible cases. Feel free to customize it in a way which best fits your requirements.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
Speedy
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or