?
Solved

AutoFill Date Ranges base on selection

Posted on 2008-06-13
4
Medium Priority
?
206 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
[X]
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
4 Comments
 
LVL 3

Accepted Solution

by:
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
                            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

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