Search code examples
c#winformsdatetimepicker

losing focus in Date Time Picker


I'm trying to implement custom search between tow dates with other custom search fields, like text boxes and check boxes, but when I use date time picker in my search criteria I can get an accurate search, but in second time the date term will be used even I didn't use it this is because I am using > dtp_fromDate.Focused Like this:

if (dtp_FromDate.Focused)
{
    tmpPersons = ctx.People.Where(c => c.InsertDate >= dtp_FromDate.Value).ToList();
}
if (dtp_ToDate.Focused)
{
    tmpPersons=ctx.People.Where(c => c.InsertDate >= dtp_ToDate.Value).ToList();               
}

My Questions are:

1# is there a way to losing focus after the search result bounded to a view? something like: dtp_FromDate.Focused=false

2# is there a better way to implement using DateTimePicker in search criteria better than what I'm using?

thanks.

PS: I searched a lot in web for this but no result could be satisfied!


Solution

  • instead of relying on focus, what about checking if the control has a value?

    the way it is implemented in the code that you have posted it means that you can either set the from date or the to date, isnt it?

    also in the to date query, it should be <=

    this should solve your problem if I have understood your question.