我有一個要求設置一個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