C# 如何在日期时间选择器中选择时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18811406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Select time in Date Time picker
提问by Pankaj
I am using a DateTimePicker in Winforms, but it is showing only Dates to select from, not the time, How can I make the control to display like the shown in this image, so that user can select the time too.
我在 Winforms 中使用 DateTimePicker,但它只显示要从中选择的日期,而不是时间,如何使控件显示为如图所示,以便用户也可以选择时间。
I tried to add
我试着添加
dateTimePicker1.Format = DateTimePickerFormat.Time;
dateTimePicker1.CustomFormat = "MM dd yyyy hh mm ss";
but then it shows time in the Control itself, it doesn't shows the clock as displayed in the image.
但是它在控件本身中显示时间,它没有显示图像中显示的时钟。
采纳答案by King King
You can set the CustomFormat
for your DateTimePicker
:
您可以CustomFormat
为您设置DateTimePicker
:
dateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss tt";
dateTimePicker1.Format = DateTimePickerFormat.Custom;
NOTE:the code above will allow user to modify all the elements: dates and time. I think that's what you want. If you want to show the Clock
, it's a lot to do more. There are some simple Clock
control already built for you.
注意:上面的代码将允许用户修改所有元素:日期和时间。我想这就是你想要的。如果您想展示Clock
,还有很多工作要做。Clock
已经为您构建了一些简单的控件。
You can search for more with the keyword Clock control
, especially in codeproject.com
(there are tons of controls), here is just 1:
您可以使用关键字搜索更多内容Clock control
,尤其是在codeproject.com
(有大量控件)中,这里只有 1 个:
http://www.codeproject.com/Articles/10627/Yet-Another-Analog-Clock
http://www.codeproject.com/Articles/10627/Yet-Another-Analog-Clock
回答by Vaibs_Cool
timePicker = new DateTimePicker();
timePicker.Format = DateTimePickerFormat.Time;
timePicker.ShowUpDown = true;
Reference:-http://msdn.microsoft.com/en-us/library/ms229631.aspx