[英]DateTimePicker: How will I handle if the user clicked/selected date in the popup calendar or not?
I noticed that C#'s DateTimePicker is changing the date automatically when I'm changing Month or Year. 我注意到C#的DateTimePicker在更改月份或年份时会自动更改日期。 So what I did is to override the ValueChanged and it is only fired when the CloseUp event is raised.
因此,我要做的是重写ValueChanged,并且仅在引发CloseUp事件时才触发它。
The problem now is I need an event if the user actually selected date in the calendar or the user clicks outside of the control. 现在的问题是,如果用户实际上在日历中选择了日期,或者用户在控件外部单击,则需要一个事件。
Can you help me with this? 你能帮我吗? Thanks :)
谢谢 :)
In a derived class, the overriden OnValueChanged
method works for me well. 在派生类中,重写的
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 ...
}
In a derived class, the overriden OnCloseUp
method works because when you reset the datetime control, the ValueChange
event does not fire, so the CloseUp
event comes in handy. 在派生类中,重写的
OnCloseUp
方法起作用是因为在重置日期时间控件时,不会触发ValueChange
事件,因此CloseUp
事件派上了用场。
void ProductionDateSchdl_CloseUp(object sender, EventArgs e)
{
this.ProductionDateSchdl.CustomFormat = "MM/dd/yyyy";
ProductionDate = Convert.ToDateTime(this.ProductionDateSchdl.Value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.