简体   繁体   English

如何仅从用户输入监听DateTimePicker ValueChanged事件

[英]How do I only listen for DateTimePicker ValueChanged Event from user input

I use the DateTimePicker Enter Event to set the Max Date to Yesterday's Date as I do not control if the user decides to leave the application open for days. 我使用DateTimePicker Enter事件将“最大日期”设置为昨天的日期,因为我无法控制用户是否决定将应用程序打开几天。

I also use the DateTimePicker ValueChanged Event to adjust my dialogues based on those selected dates. 我还使用DateTimePicker ValueChanged事件根据这些选定的日期来调整对话。

The problem that I am facing is that every time I adjust the Max Date through the Enter Event, ValueChanged Event is also called and the value taken from the DateTimePicker is not the user value. 我面临的问题是,每次我通过Enter事件调整“最大日期”时,也会调用ValueChanged事件,并且从DateTimePicker获取的值不是用户值。 Is there anything that I can do to only listen for the input change from the user? 有什么我可以做的,只是监听用户的输入更改?

The DateTimePicker CloseUp seems to help but only if the user uses the calendar to pick the date and not manually typing it in. DateTimePicker CloseUp似乎有帮助,但前提是用户使用日历来选择日期而不是手动输入日期。

    private void dtpStartDate_Enter(object sender, EventArgs e)
    {
        // Reports cannot be generated later than today.
        dtpStartDate.MaxDate = DateTime.Now.Date.AddDays(-1.0);
    }

    private void dtpStartDate_ValueChanged(object sender, EventArgs e)
    {
        DateTime startDate = dtpStartDate.Value.Date;
        DateTime endDate = dtpEndDate.Value.Date;

        dtp_CustomExclusionFrom.MinDate = startDate;
        dtp_CustomExclusionFrom.MaxDate = endDate;

        dtp_CustomExclusionTo.MinDate = startDate;
        dtp_CustomExclusionTo.MaxDate = endDate;

        foreach (ListViewItem item in lv_CustomExclusionDates.Items)
        {
            String dateString = item.Text;
            DateTime exclusionDate = DateTime.Parse(dateString);

            if (exclusionDate < startDate ||
                exclusionDate > endDate)
            {
                lv_CustomExclusionDates.Items.Remove(item);
            }
        }
    }

I expect the ValueChanged event to only be fired by the user and not by the changes to MaxDate of the DateTimePicker. 我希望ValueChanged事件仅由用户触发,而不由DateTimePicker的MaxDate更改触发。

Use a Boolean field as a flag. 使用布尔字段作为标志。

private bool isChangedFromCode = false;


private void dtpStartDate_Enter(object sender, EventArgs e)
{
    isChangedFromCode = true;
    // Reports cannot be generated later than today.
    dtpStartDate.MaxDate = DateTime.Now.Date.AddDays(-1.0);
    isChangedFromCode = false;
}

private void dtpStartDate_ValueChanged(object sender, EventArgs e)
{
    if(isChangedFromCode) { return; }
    // rest of the code here...
}

Update 更新资料

Following our conversation in the comments - the code in your dtpStartDate_Enter event handler is not the one that fires the ValueChanged event, since it only changes the MaxDate property and not the Value property of the DateTimePicker . 在评论中我们进行对话之后dtpStartDate_Enter事件处理程序中的代码不是引发ValueChanged事件的代码,因为它仅更改MaxDate属性,而不更改DateTimePickerValue属性。
You need to wrap whatever part of your code that's setting the Value property with that Boolean flag. 您需要包装使用该布尔标志设置Value属性的代码的任何部分。
Infact, I would probably make it a method if you have multiple places in code that changes the value - and only use this method instead of directly setting the Value property: 实际上,如果您在代码中有多个位置可以更改值,并且仅使用此方法而不是直接设置Value属性,则可能将其用作方法:

private void SetDateTimePickerValue(DateTime value)
{
    isChangedFromCode = true;
    dtpStartDate.Value = value;
    isChangedFromCode = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM