C# DateTimePicker => Set and Get Date

June 18, 2017 Oclemy C# DateTimePicker 1 minute, 18 seconds

So guys here we look at C# DateTimePicker.This is our first DateTimePicker lesson so its a simple one.The aim is to see how to retrieve date from a DateTimePicker and also how to set the date. We are setting date to datetimepicker from a textbox and also retrieve it and show it there. DateTimePickers allow us an easy way to work with dates in .NET. You can easily select the date and use without going to the hassle of remembering Date formats.We want to programmatically set and get the datetimepicker value.  

How To Download and Run

  • Download the project above.
  • You'll get a zipped file,extract it.
  • Open the Visual Studio.
  • From the Menu bar click on File >Open > Project/Solution
  • Now Choose a Destination Folder, from where you have the extracted project.
  • Now Click on “OK“.
  • Done, Now edit it.

 

Our Classes

Form 1 Class

  • This is our main class of course.
  • Add DateTimePicker to your Form and a textbox and two buttons : Get and Set.

 

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 DateTimePicker
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    //GET DATE
    private void getBtn_Click(object sender, EventArgs e)
    {
      txtValue.Text = dateTimePicker1.Value.ToShortDateString();
    }

    //SET DATE
    private void setBtn_Click(object sender, EventArgs e)
    {
      dateTimePicker1.Value = DateTime.Parse("10/10/2006");

    }
  }
}

 

More

YouTube

  • Visit our channel for more examples like these.

Facebook

Oclemy,Cheers.

Comments