tags:

views:

207

answers:

3

I have a Observable collection which is bind to listview which contains date and time. When a time in the collection occurs a messagebox should be shown.

I do as

public class Reminder
{
    public string Name { get; set; }
    public string Date { get; set; }
    public string Time { get; set; }
}

ObservableCollection<Reminder> reminderList = new ObservableCollection<Reminder>();
Reminder newItem = new Reminder
                                    {
                                        Name = name,
                                        Date = date,
                                        Time = time
                                    };
reminderList.Add( newItem );

How can I show a message when a time in collection occurs?

+2  A: 

How about creating a Timer object, and in its Tick event handler getting it to iterate through the list to see anything that has expired?

Chris
+1  A: 

The simple way - start a timer, set to trigger every minute (or whatever frequency makes sense). When the timer triggers, check all items in the reminderList (assuming it's of a reasonably small size) for those items that should show a notification.

private void timer1_Tick(object sender, EventArgs e)
{
    var reminders =    from item in reminderList
           where item.Date > DateTime.Now
           select item;
    foreach (Reminder reminder in reminders)
    {
     reminderList.Remove(reminder);
     MessageBox.Show("Wake up, " + reminder.Name + "!");
    }
}

Is there a reason you've got the reminder time stored as two string fields, rather than the more standard single DateTime field? Even if you're coercing the time to strings for display, make sure you're storing them internally as something more reasonable.

Michael Petrotta
You may need to add something to use the 'item.Time' as well ;) ... and compensate for the fact that 'Date' and 'Time' appear to be defined as strings in his business object.
jerryjvl
@jerryjvl: Yeah, I meant to comment on that. Let's assume that was just sample, throwaway code. Better update the answer just in case.
Michael Petrotta
A: 

Have your Reminder class implement INotifyPropertyChanged like so:

public class Reminder : INotifyPropertyChanged
{
    private string time;

    public string Name { get; set; }
    public string Date { get; set; }
    public string Time
    {
        get
        {
            return this.time;
        }
        set
        {
            if (this.time != value)
            {
                this.time = value;
                this.OnTimeChanged();
            }
        }
    }
    protected void OnTimeChanged()
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs("Time"));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

}

Then, when you new up your Reminder object, hook into the PropertyChanged event. If the property that changed is "Time" and the time is the one you want, show your message:

        var newItem = new Reminder
        {
            Name = name,
            Date = date,
            Time = time
        };
        newItem.PropertyChanged += (o, ex) =>
        {
            if (ex.PropertyName == "Time" && newItem.Time == specificTime)
            {
                //do what you need to do
            }
        };
        reminderList.Add( newItem );
BFree
Could you please explain your code. Then I can understand the concept clearly.
Sauron