一千萬個為什麽

搜索

如何從c#winform中的datetimepicker禁用星期日?

我正在處理基於窗口的應用程序,我想從 DateTimePicker Control 禁用星期日。

是否有可能禁用星期日? 如果那麽善良幫助我。

最佳答案

嘗試這個。他們可能會更容易一些。但我有這個容易的:)

它不是禁用周日日期,而是如果你選擇的上一次日期較短,並且如果你正在下降,將你帶到星期六,那麽它會把你帶到Moday。相當於禁用星期日

//你需要以下三個變量來延遲

    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;
    }

轉載註明原文: 如何從c#winform中的datetimepicker禁用星期日?