public void RemoveSelfFromCanvas(ChemProV.UI.DrawingCanvas owner) { // Start by unsubscribing from events on the parent process unit or stream if (m_commentParent is ProcessUnitControl) { (m_commentParent as ProcessUnitControl).ProcessUnit.PropertyChanged -= this.ProcessUnitParentPropertyChanged; } else if (m_commentParent is StreamControl) { (m_commentParent as StreamControl).Stream.PropertyChanged -= this.StreamParentPropertyChanged; } m_commentParent = null; // Now unsubscribe from events on the sticky note object m_note.PropertyChanged -= this.MemNote_PropertyChanged; m_note = null; // Now do the actual removal of controls from the drawing canvas if (null != m_lineToParent) { owner.RemoveChild(m_lineToParent); m_lineToParent = null; } owner.RemoveChild(this); }
/// <summary> /// Deletes this sticky note from the workspace and adds an undo to bring it back. /// </summary> public void DeleteWithUndo(DrawingCanvas canvas) { // Get a reference to the workspace Workspace ws = canvas.GetWorkspace(); // Start by unsubscribing from events if (null != m_commentParent) { if (m_commentParent is ProcessUnitControl) { (m_commentParent as ProcessUnitControl).ProcessUnit.PropertyChanged -= ProcessUnitParentPropertyChanged; } else { (m_commentParent as PFD.Streams.StreamControl).Stream.PropertyChanged -= StreamParentPropertyChanged; } } // Get a reference to the relevant comment collection IList <StickyNote> comments; if (null == m_commentParent) { comments = ws.StickyNotes; } else if (m_commentParent is ProcessUnitControl) { comments = (m_commentParent as ProcessUnitControl).ProcessUnit.Comments; } else { comments = (m_commentParent as ChemProV.PFD.Streams.StreamControl).Stream.Comments; } // Find the index of this comment in the parent collection int commentIndex = -1; for (int i = 0; i < comments.Count; i++) { if (object.ReferenceEquals(m_note, comments[i])) { commentIndex = i; break; } } // This really should never occur, but if we didn't find the comment in the collection // then this implies that this control shouldn't be on the canvas anyway, so remove it. if (-1 == commentIndex) { canvas.RemoveChild(this); canvas.RemoveChild(m_lineToParent); return; } // Add the undo first ws.AddUndo(new UndoRedoCollection( "Undo deleting comment", new InsertComment(comments, m_note, commentIndex))); // Remove the comment from the collection. Event handlers will update the UI and remove // this control (and the line to the parent control) from the drawing canvas. comments.RemoveAt(commentIndex); }