Catching Appointments Deletion

4 posts, 1 answers
  1. Christophe Grasso
    Christophe Grasso avatar
    2 posts
    Member since:
    Feb 2010

    Posted 15 Feb 2011 Link to this post

    I guys,

    I need to react to Appointment changes in the scheduler. In order to do so, I subscribed to the CollectionChanged event declared by the Appointments collection in the scheduler :

    radScheduler1.Appointments.CollectionChanged += new NotifyCollectionChangedEventHandler(Appointments_CollectionChanged);

    I have no problem on Add and ItemChanged actions, but I can't find the Appointment object in the e.OldItems collection on Remove action. I just keep on getting a null reference exception :

    public event ControlAddInEventHandler OnAppointmentsCollectionChanged;
    void Appointments_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                try
                {
                    if (OnAppointmentsCollectionChanged != null) {
                        switch (e.Action) {
                            case NotifyCollectionChangedAction.Add:
                                foreach (Appointment ap in e.NewItems)
                                {
                                    OnAppointmentsCollectionChanged(1, ap.Subject);
                                }                   
                            break;
                            case NotifyCollectionChangedAction.ItemChanged:
                            case NotifyCollectionChangedAction.Remove:
                                foreach (Appointment ap in e.OldItems)
                                {
                                    OnAppointmentsCollectionChanged(2, ap.Subject);
                                }
                            break;
                            default: break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    OnAppointmentsCollectionChanged(3, ex.Message);
                }            
            }

    Thank you for your help :)
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 15 Feb 2011 Link to this post

    Hello,

    You can catch the appointment that is being removed in the following way:

    void Appointments_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            Appointment app = (Appointment)e.NewItems[0];
            MessageBox.Show(app.Subject);
        }
    }

    E.g. Create some appointments, then highlight one and press delete. The messagebox will show thesubject of the appointment that is being deleted.
    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Christophe Grasso
    Christophe Grasso avatar
    2 posts
    Member since:
    Feb 2010

    Posted 15 Feb 2011 Link to this post

    Thank you for the quick answer Richard ! Everything works fine now !

    According to http://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedeventargs.olditems(v=vs.90).aspx
    , the deleted appointment should be found in e.OldItems... that's odd but as long as I can find it in e.NewItems, I don't really care.
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 15 Feb 2011 Link to this post

    Glad I could be of help.
    All the best
    Richard
Back to Top