问题
I want to use DateTimePicker in my Form, but I need user to select date only w/ DropDown and I'm trying to disable picking in DateTimePicker itself (e.g. by pressing KeyUp/Down).
//this.dtp_datum_pracVykaz.ValueChanged += new System.EventHandler(this.dtp_datum_pracVykaz_ValueChanged);
this.dtp_datum_pracVykaz.DropDown += dtp_datum_pracVykaz_DropDown;
this.dtp_datum_pracVykaz.CloseUp += dtp_datum_pracVykaz_CloseUp;
Where
void dtp_datum_pracVykaz_DropDown(object sender, System.EventArgs e)
{
this.dtp_datum_pracVykaz.ValueChanged += new System.EventHandler(this.dtp_datum_pracVykaz_ValueChanged);
}
void dtp_datum_pracVykaz_CloseUp(object sender, System.EventArgs e)
{
this.dtp_datum_pracVykaz.ValueChanged -= new System.EventHandler(this.dtp_datum_pracVykaz_ValueChanged);
}
The problem is that DTP is enabled, so user can change date without DropDown (but it won't have effect). I want something like DTP.Enabled = False; but icon for DropDown must be enabled.
回答1:
Use the keydown event -
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
This way any key pressed by the user will be ignored effectively disabling user keyboard input.
来源:https://stackoverflow.com/questions/15877607/datetimepicker-selection-only-with-dropdown