DateTimePicker selection only with DropDown

大憨熊 提交于 2021-02-19 07:01:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!