我正在开发基于窗口的应用程序,我想从DateTimePicker Control
.
可以禁用星期天吗?如果是这样,请帮助我。
尝试这个。他们可能是一些更简单的解决方案。但我有这个很容易:)
它不会禁用星期天日期,而是如果您选择的最后一个日期较小,它会将您带到星期一,如果您要下来,它会将您带到星期六。相当于禁用星期天
// 你需要以下三个变量来 deare
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;
}
您可以使用DateEdit
控制来更改您的任何功能DateTimePicker Control
... 检查这里。