Solved

AutoFill Date Ranges base on selection

Posted on 2008-06-13
4
198 Views
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

Thanks

   
0
Comment
Question by:JaimeJegonia
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
Pigtor earned 500 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
                            break;
                        case 1:
                            if (date1) selectedDate = new DateTime(today.Year, today.Month, 1); //1st Day of this Month
                            else selectedDate = today;  //Today's date
                            break;
                        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
                            break;
                        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
                            break;
                    }


I hope this is what you are looking for

Victor

0
 

Author Comment

by:JaimeJegonia
ID: 21786768
Victor,

Thank for answer, I'll try it.

Jimi

0
 

Author Comment

by:JaimeJegonia
ID: 21788308
Victor,

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

Thanks,

Jimi
   
0
 

Author Closing Comment

by:JaimeJegonia
ID: 31467149
Smart gist!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now