我注意到当我更改月份或年份时,C# 的 DateTimePicker 会自动更改日期。所以我所做的就是重写 ValueChanged 并且它仅在引发 CloseUp 事件时触发。
现在的问题是,如果用户实际在日历中选择了日期或用户在控件之外单击,我需要一个事件。
你能帮我吗?谢谢:)
在派生类中,重写的
OnValueChanged
方法对我来说效果很好。
public class MyDateTimePicker : DateTimePicker
{
// ... some stuff
protected override void OnValueChanged(EventArgs eventargs)
{
System.Diagnostics.Debug.Write("Clicked - ");
System.Diagnostics.Debug.WriteLine(this.Value);
base.OnValueChanged(eventargs);
}
protected override void OnCloseUp(EventArgs eventargs)
{
System.Diagnostics.Debug.Write("Closed - ");
System.Diagnostics.Debug.WriteLine(this.Value);
base.OnCloseUp(eventargs);
}
// some more stuff ...
}
在派生类中,重写
OnCloseUp
方法有效,因为当您重置日期时间控件时,ValueChange
事件不会触发,因此 CloseUp
事件会派上用场。
void ProductionDateSchdl_CloseUp(object sender, EventArgs e)
{
this.ProductionDateSchdl.CustomFormat = "MM/dd/yyyy";
ProductionDate = Convert.ToDateTime(this.ProductionDateSchdl.Value);
}