0% found this document useful (0 votes)
15 views6 pages

Date Range Selector for Weekdays

This document contains code for a C# Windows Forms application that calculates dates within a given date range based on input parameters for the start day of week, week index, and month skip. The form contains controls to select the start/end dates, day of week, week index, and month skip. When any of these controls change value, the GetDates method is called to populate the dates in the given range matching the criteria.

Uploaded by

patel saurabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Date Range Selector for Weekdays

This document contains code for a C# Windows Forms application that calculates dates within a given date range based on input parameters for the start day of week, week index, and month skip. The form contains controls to select the start/end dates, day of week, week index, and month skip. When any of these controls change value, the GetDates method is called to populate the dates in the given range matching the criteria.

Uploaded by

patel saurabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DatesInDateRange

[Link]

using System;
using [Link];

namespace DatesInDateRange
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

[Link] = 0;
[Link] = 0;
[Link] = 0;
}

/// <summary>
/// Calculates dates according to the input parameters and adds dates to ListBox.
/// </summary>
/// <param name="startDate">Start range date</param>
/// <param name="endDate">End range date</param>
/// <param name="day">N Day Of Week to get date of. Example: 0(Sunday)</param>
/// <param name="index">Nth week</param>
/// <param name="skip">Skip n months</param>
public void GetDates(DateTime startDate, DateTime endDate, int day, int index,
int skip)
{
//Refreshes ListBox to populate again
[Link]();

//Starts looping from the 1st date of the StartDate month to get appropriate
week and day
DateTime date = new DateTime([Link], [Link], 1);

while (date <= endDate)


{
bool success = true;

int i = [Link] == 0 ? index - 1 : index;

//Adds days to the date to get the desired date


date = [Link]((7 * (i)) - (int)[Link] + day);

//Skips the start month if the desired date is passed and then continues
from the next month
if (date < startDate)
{
date = [Link](1).AddDays(-[Link]);
success = false;
}

if (success)
{
if ((int)[Link] == [Link] && date >= startDate
&& date <= endDate)
{
[Link]([Link]());
}

date = new DateTime([Link], [Link], 1);


date = [Link](skip);
}
}
}

public void CallGetDates()


{
try
{
string skipTrimmed = [Link]().Replace(" months",
"").Replace(" month", "").Replace(" year", "2").Trim();
int skip = [Link](skipTrimmed);
DateTime start = new DateTime([Link], [Link],
[Link], 0, 0, 0);
DateTime end = new DateTime([Link], [Link],
[Link], 0, 0, 0);
GetDates(start, end, [Link], [Link] + 1,
skip);
}
catch { }
}

#region [[ Events ]]
private void dtpStart_ValueChanged(object sender, EventArgs e)
{
CallGetDates();
}

private void dtpEnd_ValueChanged(object sender, EventArgs e)


{
CallGetDates();
}

private void cbDay_SelectedIndexChanged(object sender, EventArgs e)


{
CallGetDates();
}

private void cbIndex_SelectedIndexChanged(object sender, EventArgs e)


{
CallGetDates();
}

private void cbSkip_SelectedIndexChanged(object sender, EventArgs e)


{
CallGetDates();
}
#endregion
}
}

[Link]
namespace DatesInDateRange
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private [Link] components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed;
otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
[Link]();
}
[Link](disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new [Link]();
this.label2 = new [Link]();
this.label3 = new [Link]();
this.label4 = new [Link]();
this.label5 = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link] = new [Link]();
[Link]();
//
// label1
//
[Link] = true;
[Link] = new [Link](16, 32);
[Link] = "label1";
[Link] = new [Link](29, 13);
[Link] = 0;
[Link] = "Start";
//
// label2
//
[Link] = true;
[Link] = new [Link](16, 67);
[Link] = "label2";
[Link] = new [Link](26, 13);
[Link] = 1;
[Link] = "End";
//
// label3
//
[Link] = true;
[Link] = new [Link](16, 102);
[Link] = "label3";
[Link] = new [Link](26, 13);
[Link] = 2;
[Link] = "Day";
//
// label4
//
[Link] = true;
[Link] = new [Link](16, 137);
[Link] = "label4";
[Link] = new [Link](33, 13);
[Link] = 3;
[Link] = "Index";
//
// label5
//
[Link] = true;
[Link] = new [Link](16, 172);
[Link] = "label5";
[Link] = new [Link](28, 13);
[Link] = 4;
[Link] = "Skip";
//
// dtpStart
//
[Link] = [Link];
[Link] = new [Link](55, 28);
[Link] = "dtpStart";
[Link] = new [Link](96, 20);
[Link] = 5;
[Link] += new
[Link](this.dtpStart_ValueChanged);
//
// dtpEnd
//
[Link] = [Link];
[Link] = new [Link](55, 63);
[Link] = "dtpEnd";
[Link] = new [Link](96, 20);
[Link] = 6;
[Link] += new
[Link](this.dtpEnd_ValueChanged);
//
// cbDay
//
[Link] = true;
[Link](new object[] {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"});
[Link] = new [Link](55, 99);
[Link] = "cbDay";
[Link] = new [Link](96, 21);
[Link] = 7;
[Link] += new
[Link](this.cbDay_SelectedIndexChanged);
//
// cbIndex
//
[Link] = true;
[Link](new object[] {
"1st",
"2nd",
"3rd",
"4th"});
[Link] = new [Link](55, 134);
[Link] = "cbIndex";
[Link] = new [Link](96, 21);
[Link] = 8;
[Link] += new
[Link](this.cbIndex_SelectedIndexChanged);
//
// cbSkip
//
[Link] = true;
[Link](new object[] {
"1 Month",
"2 Months",
"4 Months",
"6 Months",
"1 Year"});
[Link] = new [Link](55, 169);
[Link] = "cbSkip";
[Link] = new [Link](96, 21);
[Link] = 9;
[Link] += new
[Link](this.cbSkip_SelectedIndexChanged);
//
// listDates
//
[Link] = true;
[Link] = new [Link](173, 15);
[Link] = "listDates";
[Link] = new [Link](177, 199);
[Link] = 10;
//
// Form1
//
[Link] = new [Link](6F, 13F);
[Link] = [Link];
[Link] = new [Link](362, 228);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](this.label5);
[Link](this.label4);
[Link](this.label3);
[Link](this.label2);
[Link](this.label1);
[Link] = [Link];
[Link] = "Form1";
[Link] = [Link];
[Link] = "Dates Within Date Range";
[Link](false);
[Link]();

#endregion

private [Link] label1;


private [Link] label2;
private [Link] label3;
private [Link] label4;
private [Link] label5;
private [Link] dtpStart;
private [Link] dtpEnd;
private [Link] cbDay;
private [Link] cbIndex;
private [Link] cbSkip;
private [Link] listDates;
}
}

You might also like