How to set a range for the Max and Min Date/Time Picker

Viewed 712

I am wondering how to set a max date time selection for the date time picker element in C sharp windows forms. I want to have a limit for how far in the past or in the future that a date can be, but I'm not sure how to do it. I don't know if my code will be of use but i have attached it here anyway. Thanks in advance.

To give a light rundown of what the program is doing, pretty much for my coursework I have to make a system with sql tables that can carry out various functions, this range problem is for the Booking form in the project so the user can select a date and time for the booking slot that they would like to use, so I want to have a limit- ideally the user shouldn't be able to create a booking with a date in the past, nor should they be able to create a booking for a ridiculous time ahead in the future, like 50 years time.

Really appreciate any help and I understand if this is a simple problem to solve for some of the more experienced coders out there. thanks.

public partial class AddBooking : Form
{
    private int count;
    private Boolean IsEmpty = false;
    private static string _connectionstring = ConfigurationManager.ConnectionStrings["DoggieConnectionString"].ConnectionString;
    
    public AddBooking()
    {
        InitializeComponent();
        CenterToScreen();
        GenerateBookingNumber();
        IDDisplay.Text = "" + count;
        dateTimePicker2.Format = DateTimePickerFormat.Custom;
        dateTimePicker2.CustomFormat = "HH:mm tt";
        dateTimePicker2.ShowUpDown = true;
        DateTime now = DateTime.Now;
    }

    private void AddBooking_Load(object sender, EventArgs e)
    {
    }

    private int GenerateBookingNumber()
    {
        string smt = "SELECT COUNT(*) FROM dbo.Booking";
        count = 0;

        using (SqlConnection connection = new SqlConnection(_connectionstring))
        {
            using (SqlCommand cmdCount = new SqlCommand(smt, connection))
            {
                connection.Open();
                count = (int)Convert.ToInt32(cmdCount.ExecuteScalar());
            }
        }

        count = count + 4;
        return count;
    }

    private void PresenceCheck()
    {
        if (string.IsNullOrEmpty(WalkLocationtxt.Text) || string.IsNullOrEmpty(StaffIDtxt.Text))
        {
            IsEmpty = true;
        }
        else
        {
            IsEmpty = false;
        }
    }

    private void SubmitInfobtn_Click(object sender, EventArgs e)
    {
       // MessageBox.Show("welcome " + dateTimePicker1.Value.ToShortDateString());
       // MessageBox.Show("Goodddd" + dateTimePicker2.Value.ToShortTimeString());
        PresenceCheck();

        if (IsEmpty == false)
        {
            int rowsareaffected = ClassDatabase.AddBookingDetails(Convert.ToInt32(IDDisplay.Text), dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortTimeString(), Convert.ToInt32(StaffIDtxt.Text), WalkLocationtxt.Text);

            if (rowsareaffected > 0)
            {
                MessageBox.Show("New Booking Added Sucessfully", "Sucess!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                StaffIDtxt.Clear();
                WalkLocationtxt.Clear();
                
                GenerateBookingNumber();

                IDDisplay.Text = "" + count;
            }
            else
            {
                MessageBox.Show("An Error Occurred", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    private void returnToMenuToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new MenuScreen().Show();
        this.Close();
    }

    private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new LoginScreen().Show();
        this.Close();
    }

    private void exitSystemToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult Result = MessageBox.Show("Are you sure you want to Exit the JD Dog Care Program?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);

        if (Result == DialogResult.Yes)
        {
            Application.Exit();
        }
        else
        {
        }
    }

    private void addClientToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddClient().Show();
        this.Close();
    }

    private void manageClientsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewClient().Show();
        this.Close();
    }

    private void addDogsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddDog().Show();
        this.Close();
    }

    private void manageDogsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewDog().Show();
        this.Close();
    }

    private void addStaffToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddStaff().Show();
        this.Close();
    }

    private void manageStaffToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewStaff().Show();
        this.Close();
    }

    private void addBookingToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddBooking().Show();
        this.Close();
    }

    private void manageBookingToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewBooking().Show();
        this.Close();
    }

    private void addDogToBookingToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new DogToWalk().Show();
        this.Close();
    }
}
1 Answers

You can set Min and Max properties of DateTimePicker:

DateTime now = DateTime.Now;
DateTime minDate = now.AddYears(-50)
DateTime maxDate = now.AddYears(50)

dateTimePicker.MinDate = minDate 
dateTimePicker.MaxDate = maxDate 
Related