AutoFill Date Ranges base on selection

Posted on 2008-06-13
Medium Priority
Last Modified: 2013-12-17
Dear Experts,

I  Have a DropDown with the following Lists Thisweek, Lastweek, ThisMonth, LastMonth and the textboxes  (Date1 & Date2).

What I want by selecting DropDown List it will also populate corresponding values as follows :

Selected:                       Date1                                   Date2
ThisWeek:                    Monday's Date                     Today' Date
ThisMonth                    1st Day of this Month           Today' Date
Lastweek                    Monday of Last week           Sunday of Last week
LstMonth                     1st Day of Last Month           End of Last Month


Question by:JaimeJegonia
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

Pigtor earned 2000 total points
ID: 21785633
Copy and paste the following code in the listBox1_SelectedIndexChanged event.

bool date1 = true;   //Indicates whether to retrieve the date1 or date2 value
                    int dayOfWeek = System.Convert.ToInt32(System.DateTime.Now.DayOfWeek);
                    //The start of the week must be on monday, for that reason we move one day back to the day of week variable.
                    if (dayOfWeek == 0) dayOfWeek = 6;  
                    else dayOfWeek -= 1;

                    DateTime selectedDate = DateTime.Now;  //This variable holds the calculated date.                  
                    DateTime today = System.DateTime.Now.Date;
                    switch (listBox1.SelectedIndex)
                        case 0:
                            if (date1) selectedDate = today.AddDays(dayOfWeek * -1);  //Monday's Date  
                            else selectedDate = today;  //Today's date
                        case 1:
                            if (date1) selectedDate = new DateTime(today.Year, today.Month, 1); //1st Day of this Month
                            else selectedDate = today;  //Today's date
                        case 2:
                            //Move back one week and calculate monday and sunday...
                            DateTime oneWeekBack = today.AddDays(-7);
                            if (date1) selectedDate = oneWeekBack.AddDays(dayOfWeek * -1); //Monday of Last week
                            else selectedDate = oneWeekBack.AddDays((6 - dayOfWeek)); //Sunday of Last week
                        case 3:
                            //Move back one month and calculate the first and last day...
                            DateTime oneMonthBack = today.AddMonths(-1);
                            if (date1) selectedDate = new DateTime(oneMonthBack.Year, oneMonthBack.Month, 1); //1st Day of Last Month
                            else selectedDate = new DateTime(oneMonthBack.Year, oneMonthBack.Month, DateTime.DaysInMonth(oneMonthBack.Year, oneMonthBack.Month)); //End of Last Month

I hope this is what you are looking for



Author Comment

ID: 21786768

Thank for answer, I'll try it.



Author Comment

ID: 21788308

Great!  It  needs only few tweaks to meet my requirement.



Author Closing Comment

ID: 31467149
Smart gist!

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question