我正在开发基于窗口的应用程序,我想从DateTimePicker Control
禁用星期日。
可以禁用星期日吗? 如果有这么好的帮助我。
答案 0 :(得分:1)
试试这个。他们可能是一些更容易的解决方但我觉得这很容易:)
如果您选择的最后一个日期较小,并且如果您要下来,则会将您带到星期六。相当于禁用星期日
//你需要跟随三个变量来进行延伸
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;
}
答案 1 :(得分:0)
您可以使用DateEdit
控制来更改DateTimePicker Control
的任何功能...
检查here。