Reputation: 63
i'm working on window based application i want to disable Sunday from DateTimePicker Control
.
is it possible to disable Sunday? if so kindly help me.
Upvotes: 0
Views: 2878
Reputation: 8419
Try this. Their may be some easier solution. But I have this as easy one :)
It is not disabling Sunday dates rather it takes you to Moday if your last date selected was lesser and takes you to Saturday if you are coming down. Equivalent to disabling sunday
// You need following three variables to delare
DateTime lastDate;
System.Text.RegularExpressions.Regex rg;
bool valueChangingProgramatically = false;
//If you have already Form_load Event, just add the body of following event in yours
// Add form_load event and write this code in body
private void Form1_Load(object sender, EventArgs e)
{
lastDate = dateTimePicker1.Value;
rg = new System.Text.RegularExpressions.Regex("Sunday");
if (lastDate < dateTimePicker1.Value)
dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1);
// I am adding event of datetimepicker value changed at the end of FormLoad
dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
}
// valueChangingProgramatically avoids recursion/ infinte loop/repeteion of this event
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (!valueChangingProgramatically)
{
if (rg.IsMatch(dateTimePicker1.Value.ToLongDateString()))
{
valueChangingProgramatically = true;
if (lastDate < dateTimePicker1.Value)
dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1);
else
dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-1);
}
}
else
valueChangingProgramatically = false;
lastDate = dateTimePicker1.Value;
}
Upvotes: 1
Reputation: 7040
You can use DateEdit
control to change any functionality of your DateTimePicker Control
...
Check here.
Upvotes: 0