简体   繁体   中英

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.

I also use the DateTimePicker ValueChanged Event to adjust my dialogues based on those selected dates.

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. 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.

    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.

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 .
You need to wrap whatever part of your code that's setting the Value property with that Boolean flag.
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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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