DateTimePicker只允许用户通过单击日历来选择日期

时间:2017-08-22 20:30:31

标签: c# .net winforms datetimepicker

我遇到了dateTimePicker课程的问题。我需要在构造上使用DateTimePicker空,我通过这个来实现:

dateTimePicker1.MaxDate = DateTime.Now; // Ensures no future dates are set.
dateTimePicker1.Format = DateTimePickerFormat.Custom;   
dateTimePicker1.CustomFormat = " ";

我的问题是,现在用户必须在日历上实际点击以输入日期,而不是使用向上键,或键入。无论如何,我可以像我在构造函数中那样初始化dateTimePicker1" ",但也允许用户使用箭头键来“激活”它而不是点击日历?

我的ValueChanged更改了活动

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
            {
                dateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm:ss";
                if (dateTimePicker1.Checked == true)
                {
                    dateTimePicker1.Enabled = true;
                    dateTimePicker1.Format = DateTimePickerFormat.Short;
                }
                else if (dateTimePicker1.Checked == false)
                {
                    dateTimePicker1.Enabled = false;
                    dateTimePicker1.Format = DateTimePickerFormat.Custom;
                    dateTimePicker1.CustomFormat = " ";
                }
            }

我知道DateTimePicker不容易为空,因此你必须对类的扩展进行修改,但这是一个快速修复。任何想法将不胜感激。

0 个答案:

没有答案