C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: Please click the lightning bolt. Add the Form1_Load event handler by double-clicking on the Form.
Load: Here we get the DateTime value for yesterday. This is done with Today, Subtract, and TimeSpan. The Value property is assigned to the result.
DateTimeDateTime.TodayDateTime SubtractTimeSpanValueChanged: Whenever the user changes the DateTime, this event handler is executed. The window's title shows the DateTime that was selected.
C# program that uses DateTimePicker
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Initialize picker to yesterday.
DateTime result = DateTime.Today.Subtract(TimeSpan.FromDays(1));
dateTimePicker1.Value = result;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
// Set title bar to selected date.
DateTime result = dateTimePicker1.Value;
this.Text = result.ToString();
}
}
}
Tip: It is possibly a good idea to set ShowUpDown to True when the DateTime will only need to be changed up or down by a couple units.