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
Solved

AutoFill Date Ranges base on selection

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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