Azeem Raza Tayyab
Azeem Raza Tayyab

Reputation: 63

How to disable sunday from datetimepicker in c# winform?

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

Answers (2)

Sami
Sami

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

Gopesh Sharma
Gopesh Sharma

Reputation: 7040

You can use DateEdit control to change any functionality of your DateTimePicker Control... Check here.

Upvotes: 0

Related Questions