0

I'd like to display message in alertbox based on how many days left till the deadline, say if there is one day left, the alertbox comes out and say "One day left of your renewal date!". I use the following code

 if ((RenewalDate.Value - DateTime.Now).TotalDays == 5)
                            MessageBox.Show("Your deadline is within 5 days");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 4)
                            MessageBox.Show("Your deadline is 4 days left");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 3)
                            MessageBox.Show("Your deadline is 3 days left");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 2)
                            MessageBox.Show("Your deadline is 2 days left");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 1)
                            MessageBox.Show("Your deadline is 1 days left");

But it does not work. I have no idea why. Any ideas? Thanks in advance. My code as follows:

private int _OrganisationID = 1;
private DateTime? _RenewalDate;

public event PropertyChangedEventHandler PropertyChanged;

[Required(ErrorMessage = "OrganisationID is required.")]
public int OrganisationID
{
    get { return _OrganisationID; }
    set
    {
        if (_OrganisationID == value)
            return;

        _OrganisationID = value;
        PropertyChanged(this, new PropertyChangedEventArgs("OrganisationID"));
    }
}


[Required(ErrorMessage = "RenewalDate is a required field.")]
public DateTime? RenewalDate
{
    get { return _RenewalDate; }
    set
    {

        _RenewalDate = value;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("RenewalDate"));
        }
    }
}


DispatcherTimer timer = new DispatcherTimer();
//  DateTime myDeadLine = new DateTime();
public void InitTimer()
{
    // Checks every minute
    timer.Interval = new TimeSpan(0, 1, 0);
    timer.Tick += timer_Tick;
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    //if (( RenewalDate.Value - DateTime.Now).TotalDays <= 1)
    //    MessageBox.Show("Your Alert Message");
}

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.Source is TabControl)
    {
        if (ClientDeadLines.IsSelected)
        {
            using (var context = new ProActiveDBEntities())
            {
                var org = context.Organisations.Where(o => o.OrganisationID == this.OrganisationID).FirstOrDefault();

                if (org != null)
                {

                    RenewalDate = org.RenewalDate;

                    if ((RenewalDate.Value - DateTime.Now).TotalDays <= 2)
                        MessageBox.Show("Your Alert Message");
                }
                else
                    {
                    MessageBox.Show("Unable to retrieve data, please set up organsiation first.");
                }
            }

        }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
DanielXu
  • 97
  • 4
  • 14
  • Please post the error you got... – Naren Aug 28 '13 at 08:15
  • 1
    @Naren. Hi I fix the problem by changing DateTime.Now to DateTime.Today. thanks all the same. – DanielXu Aug 28 '13 at 08:35
  • @Naren. Hi Naren. I am a freshman in wpf and I met a run time error in my app while achieving the function "hyperlink email". Can you please take a look at my another question from http://stackoverflow.com/questions/18400363/runtime-error-wpf-ioexception-cannot-locate-resource I don't know whether it is ok for @ you to solve my questions here. Sorry if not. – DanielXu Aug 28 '13 at 10:50
  • I too am new to WPF.I will try to solve it. – Naren Aug 28 '13 at 10:54
  • @Naren. Thanks for your reply. Do you find the problem? – DanielXu Aug 28 '13 at 14:31

1 Answers1

2

Did you even try to debug this problem? Put a break point on the first line:

if ((RenewalDate.Value - DateTime.Now).TotalDays == 5)

What does ((RenewalDate.Value - DateTime.Now).TotalDays equal???

((RenewalDate.Value - DateTime.Now).TotalDays will probably never equal 5 because TotalDays is a double. Try using the Days property instead.

UPDATE >>>

Also, did you know that you could approximately refactor your code to the following?:

TimeSpan daysRemaining = RenewalDate.Value - DateTime.Now;
if (daysRemaining.Days <= 5) MessageBox.Show(string.Format("Your deadline is {0} days 
away", daysRemaining.Days));
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Thanks for your suggestion. yes, as you said the minus result does not equal to 5 if I use DateTime.Now. I fix the problem by changing DateTime.Now to DateTime.Today. My first time using breakpoint, so happy fixing the problem. – DanielXu Aug 28 '13 at 08:25
  • I still use TotalDays instead of Days based on my own project needs, but now I know the differences between them. Thanks. – DanielXu Aug 28 '13 at 08:34