Partager via


UndoContext Interface

Represents, as a single transaction, all operations performed on all participating open documents in Visual Studio. If its SetAborted method is invoked, all changes made since opening the object are discarded.

Namespace:  EnvDTE
Assembly:  EnvDTE (in EnvDTE.dll)

Syntax

'Declaration
<GuidAttribute("D8DEC44D-CAF2-4B39-A539-B91AE921BA92")> _
Public Interface UndoContext
[GuidAttribute("D8DEC44D-CAF2-4B39-A539-B91AE921BA92")]
public interface UndoContext
[GuidAttribute(L"D8DEC44D-CAF2-4B39-A539-B91AE921BA92")]
public interface class UndoContext
[<GuidAttribute("D8DEC44D-CAF2-4B39-A539-B91AE921BA92")>]
type UndoContext =  interface end
public interface UndoContext

The UndoContext type exposes the following members.

Properties

  Name Description
Public property DTE Gets the top-level extensibility object.
Public property IsAborted Gets whether the UndoContext object operation was terminated by the SetAborted method.
Public property IsOpen Gets whether an undo operation is currently in effect or if a solution is open.
Public property IsStrict Gets whether the undo stack linkage is strict.
Public property Parent Gets the immediate parent object of an UndoContext object.

Top

Methods

  Name Description
Public method Close Ends an undo operation.
Public method Open Starts a new undo operation.
Public method SetAborted Discards all changes to participating open documents since opening the UndoContext object.

Top

Remarks

The UndoContext object is returned by the UndoContext property of the _DTE object. There is only one global undo service in Visual Studio that is either open or closed. After the UndoContext object is opened, all updates made to documents in Visual Studio can be reversed (undone) by a single undo action, the SetAborted method, until the object is closed. If the SetAborted method is invoked, all changes made since opening the object are discarded.

To use the UndoContext object, open it, make document changes, and then close the object to finalize the changes.

To discard the changes, call the SetAborted method before closing the UndoContext object. After you close UndoContext object, you can no longer undo the changes.

Note

Before using the UndoContext object, check to see if it is already open from a previous operation. If it is already open, then your changes will be undone in all participating documents, along with all changes done since the object was first opened. If the UndoContext object is already open when you check it, do not close it, because the previous caller still requires it for an undo operation. Consequently, you should not call SetAborted and then close the UndoContext object unless you originally opened it. Close it after you are done changing text in documents.

Examples

Sub UndoContextExample()
   ' Before running, select text in an open document.
   Dim txtSel As TextSelection
   Dim strTS As String, boolWasOpen As Boolean
   txtSel = DTE.ActiveDocument.Selection
   strTS = txtSel.Text

   ' Check to see if UndoContext object is already open.
   If DTE.UndoContext.IsOpen = True Then
      boolWasOpen = True
   Else
      ' Open the UndoContext object to track changes.
      DTE.UndoContext.Open("RemoveNewLines", False)
   End If

   ' Perform search for newline characters and remove them.
   If strTS <> "" Then
      txtSel.Delete()
      strTS = Replace(strTS, vbNewLine, "", Compare:=vbTextCompare)
      txtSel.Insert(strTS)
   End If

   ' If UndoContext was already open, do not close it.
   If boolWasOpen = False Then
      ' Close the UndoContext object to commit the changes.
      DTE.UndoContext.Close()
   End If
End Sub

See Also

Reference

EnvDTE Namespace