我有一个要求设置一个C#DateTimePicker
控制范围内更改,我希望它只显示小时:分钟:第二,我希望最小值为10秒。DateTimePicker类更改范围
E.g.
if hour == 0 && min == 0
then second should be limited to change within 10 ~ 59
else second can change within 0 ~ 59
我该怎么做?
我有一个要求设置一个C#DateTimePicker
控制范围内更改,我希望它只显示小时:分钟:第二,我希望最小值为10秒。DateTimePicker类更改范围
E.g.
if hour == 0 && min == 0
then second should be limited to change within 10 ~ 59
else second can change within 0 ~ 59
我该怎么做?
您需要将事件onValueChanged添加到datePicker控件。这是应该工作的代码。此代码适用于获胜形式。
public Form1()
{
InitializeComponent();
if (dt.Seconds % 10 > 0)
{
initialValue = true;
dateTimePicker1.Value = dt.Seconds (-(dt.Seconds % 10));
}
_prevDate = dateTimePicker1.Value;
}
private DateTime _prevDate;
private bool initialValue = false;
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if(initialValue)
{
initialValue = false;
return;
}
DateTime dt = dateTimePicker1.Value;
TimeSpan diff = dt - _prevDate;
if (diff.Ticks < 0)
dateTimePicker1.Value = _prevDate.AddSeconds(-10);
else
dateTimePicker1.Value = _prevDate.AddSeconds(10);
_prevDate = dateTimePicker1.Value;
}
您需要将它添加到日期选择器:
this.dateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm:ss";
this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.dateTimePicker1.ShowUpDown = true;
这有助于我学习和解决问题。谢谢 – Charlie 2014-10-06 10:19:20
@Charlie欢迎您,祝您的项目或任务顺利! :) – mybirthname 2014-10-06 10:23:25
I played a while, here is the solution:
namespace DataTimePicker1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTime dt = dateTimePicker1.Value;
if (dt.Hour == 0 && dt.Minute == 0 &&
dt.Second < 10)
{
int inc = 10 - dt.Second;
dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(inc);
}
}
}
}
为了什么? WPF? ASP.NET?的WinForms?你有没有尝试过任何可以发布的内容(即使它目前不工作)? – 2014-10-06 02:21:49