Redian新闻
>
How to cancel DateTimePicker event?
avatar
How to cancel DateTimePicker event?# DotNet - 窗口里的风景
c*t
1
There are two dateTimePickers on a winform, which requires dt2 > dt1;
if dt1 is picked greater than dt2, reset dt1 to dt2-1; but the messagebox
pops
out twice. How to disable the second one? ..EnableEvents seems not working
//The code is as follows.
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
{
MessageBox.Show("This date should NOT be earlier then " +
this.dateTimeP
avatar
k*i
2
Per your description, the code should be changed as follows:
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
{
MessageBox.Show("This date should NOT be earlier then " +
this.dateTimePicker1.Value.ToShortDateString() + "!");
this.Application.EnableEvents = false;
this.dateTimePicker1.Value = this.dateTimePicker2.Value.
AddDays(-1);


【在 c**t 的大作中提到】
: There are two dateTimePickers on a winform, which requires dt2 > dt1;
: if dt1 is picked greater than dt2, reset dt1 to dt2-1; but the messagebox
: pops
: out twice. How to disable the second one? ..EnableEvents seems not working
: //The code is as follows.
: private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
: {
: if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
: {
: MessageBox.Show("This date should NOT be earlier then " +

avatar
c*t
3
the messagebox is still called twice.
I tried both:
this.Application.EnableEvents = false;
this.dateTimePicker1.Value = this.dataTimePicker2.Value.AddDays(-1);
this.Application.EnableEvents = true;
and
Globals.ThisWorkbook.Application.EnableEvents = false;
..
Globals.ThisWorkbook.Applicaiton.EnableEvents = true;
Neither worked.

【在 k****i 的大作中提到】
: Per your description, the code should be changed as follows:
: private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
: {
: if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
: {
: MessageBox.Show("This date should NOT be earlier then " +
: this.dateTimePicker1.Value.ToShortDateString() + "!");
: this.Application.EnableEvents = false;
: this.dateTimePicker1.Value = this.dateTimePicker2.Value.
: AddDays(-1);

avatar
c*t
4
I used a static counter, and confirmed that the popup was called twice

【在 k****i 的大作中提到】
: Per your description, the code should be changed as follows:
: private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
: {
: if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
: {
: MessageBox.Show("This date should NOT be earlier then " +
: this.dateTimePicker1.Value.ToShortDateString() + "!");
: this.Application.EnableEvents = false;
: this.dateTimePicker1.Value = this.dateTimePicker2.Value.
: AddDays(-1);

avatar
k*i
5
Does the dateTimePicker1.Value change between these two calls?

【在 c**t 的大作中提到】
: I used a static counter, and confirmed that the popup was called twice
avatar
c*t
6
obviously!

【在 k****i 的大作中提到】
: Does the dateTimePicker1.Value change between these two calls?
avatar
c*t
7
found solution!
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
this.dateTimePicker1.MaxDate = this.dateTimePicker2.Value;
..
}
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
this.dateTimePicker2.MinDate = this.dateTimePicker1.Value;
..
}

working

【在 c**t 的大作中提到】
: There are two dateTimePickers on a winform, which requires dt2 > dt1;
: if dt1 is picked greater than dt2, reset dt1 to dt2-1; but the messagebox
: pops
: out twice. How to disable the second one? ..EnableEvents seems not working
: //The code is as follows.
: private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
: {
: if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
: {
: MessageBox.Show("This date should NOT be earlier then " +

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。