Here’s an example of how you can set up a MaskedTextBox to allow date input in the format yyyy-MM-dd or yyyy/MM/dd:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// Create a MaskedTextBox
MaskedTextBox maskedTextBox = new MaskedTextBox
{
// Set the mask to accept date in yyyy-MM-dd or yyyy/MM/dd format
Mask = "0000-00-00",
// Set the prompt character to a space
PromptChar = ' ',
// Set the valid input to digits and the date separator characters
ValidChars = "0123456789-/",
// Set the location and size of the MaskedTextBox
Location = new System.Drawing.Point(50, 50),
Size = new System.Drawing.Size(100, 20)
};
// Add the MaskedTextBox to the form
Controls.Add(maskedTextBox);
}
}
}
In this example, we've set the mask to "0000-00-00" to allow input in the yyyy-MM-dd format. We've also set the ValidChars property to "0123456789-/", which means the user can only enter digits and the '-' or '/' characters. The PromptChar is set to a space, which will be displayed in place of the mask characters until the user enters something.
You can also use the TypeValidationCompleted event to perform additional validation on the input and provide feedback to the user.