C# DateTimePicker

How To Use A DateTimePicker Control In C#

Using DateTimePicker In C#

In This C# Tutorial We Will See How To Use DateTimePicker Control To Do The Following Things:
- customize the date format.
- set a default date.
- set a minimum date.
- set a maximum date.
- add days.
- add months.
- add years.
- get the full date.
- get day value.
- get month value.
- get year value.
Using Csharp Programming Language And Visual Studio Editor.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csharp_Tutorials
{
    public partial class Using_DateTimePicker : Form
    {
        public Using_DateTimePicker()
        {
            InitializeComponent();
        }

        private void Using_DateTimePicker_Load(object sender, EventArgs e)
        {
            // custom date format
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "MM-dd-yyyy";
            
            // set date
            dateTimePicker1.Value = new DateTime(2000, 7, 10);

            // min date
            dateTimePicker1.MinDate = new DateTime(1900, 1, 1);

            // max date
            dateTimePicker1.MaxDate = new DateTime(2500, 1, 1);

        }

        private void buttonADD_Click(object sender, EventArgs e)
        {

            // add days to the dateTimePicker date
            // dateTimePicker1.Value = dateTimePicker1.Value.AddDays(4);

            // add months to the dateTimePicker date
            // dateTimePicker1.Value = dateTimePicker1.Value.AddMonths(4);

            // add years to the dateTimePicker date
            // error from the max date

             dateTimePicker1.Value = dateTimePicker1.Value.AddYears(4);
        }

        private void buttonGET_Click(object sender, EventArgs e)
        {
            // get full date
            textBox1.Text = dateTimePicker1.Value.ToString();

            // get day value
            textBox1.Text = dateTimePicker1.Value.Day.ToString();

            // get month value
            textBox1.Text = dateTimePicker1.Value.Month.ToString();

            // get year value
            textBox1.Text = dateTimePicker1.Value.Year.ToString();
        }
    }
}

      




Share this

Related Posts

Previous
Next Post »