问题描述
我是c#的新手,在我的程序中我使用DateTimePicker值更改事件,但我发现当用户单击箭头或以编程方式更改值时也会发生ValueChanged事件,我只想识别用户交互的DateTimePicker(不是通过编程方式更改值时),有什么方法可以做到这一点?
I'm new to c#, in my program im using DateTimePicker Value changed event but i found ValueChanged event occurs when the user clicks on arrow or if the value is changed programatically as well, I want to identify only the user interacts of the DateTimePicker (not when the value is changed programatically), Is there any way to do this?
推荐答案
是的,看看。尤其是 OnValueChanged
事件
Yes, take a look at the MSDN documentation. Especially, the OnValueChanged
event
您将需要使用以下事件来连接控件:
You will need to wire your control up using this event:
在构造函数方法中:
dateTimePickerControl.ValueChanged += new EventHandler(picker_ValueChanged);
这是方法签名:
void f_ValueChanged(object sender, EventArgs e)
{
//Do whatever you need when the value changes here
}
您也可以从设计器中执行此操作。如果转到属性,然后单击事件部分,它将列出所有事件。只需双击,它将为您创建方法签名和连线。
You can also do this from the designer. If you go to the Properties, then Events portion, it lists all of the events. Just double click and it will create the method signature and wiring for you.
更新为您的更新
如果您特别想检查这是否是程序性更改,则需要执行以下操作:
If you specifically want to check whether this is a programmatic change or not, then you want to do something like this:
在以下位置创建全局变量您的班级
Create a global variable in your class
布尔值isProgrammaticEvent = false
;
在进行程序更改之前:
isProgrammaticEvent = true;
//Change picker value
在您的事件连线中:
void f_ValueChanged(object sender, EventArgs e)
{
Boolean isThisProgrammatic = isProgrammaticEvent;
isProgrammaticEvent = false;
if(isThisProgrammatic)
return;
//Do whatever you need when the value changes here
}
这篇关于用户与DateTimePicker控件交互时会引发什么事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!