The easy way is to place a TextBox and Button on the form, show a child form with a month calendar control. In the child form, select a date and click the Select button which sends the selected date to an event which the calling form places in a TextBox. If when calling the child form without a DateTime the current date is used, if there is a DateTime it's used.
Full source (calendar image is in the assets folder)
Form1
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
ActiveControl = ShowCalendarButton;
}
private void ShowCalendarButton_Click(object sender, EventArgs e)
{
CalendarForm calendarForm = null;
try
{
// decide to pass a date time or not
calendarForm = DateTime.TryParse(DateTimeValueTextBox.Text, out var currentDateTime) ?
new CalendarForm(currentDateTime) :
new CalendarForm();
// reposition to be next to calendar button
calendarForm.Location = new Point(Left + (Width - 100), Bottom - 80);
calendarForm.DateTimeHandler += CalendarFormOnDateTimeHandler;
calendarForm.ShowDialog();
}
finally
{
calendarForm.DateTimeHandler -= CalendarFormOnDateTimeHandler;
calendarForm.Dispose();
}
}
// set date time
private void CalendarFormOnDateTimeHandler(DateTime sender)
{
DateTimeValueTextBox.Text = $"{sender.ToString("MM-dd-yyyy")}";
}
}
Calendar form
public partial class CalendarForm : Form
{
public delegate void OnSelectDateTime(DateTime sender);
public event OnSelectDateTime DateTimeHandler;
public CalendarForm()
{
InitializeComponent();
}
public CalendarForm(DateTime sender)
{
InitializeComponent();
monthCalendar1.SetDate(sender);
}
private void SelectButton_Click(object sender, EventArgs e)
{
DateTimeHandler?.Invoke(monthCalendar1.SelectionRange.Start);
Close();
}
}
Cheap alternate
Create a KeyDown event for a DateTimePicker as follows
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}