Visual Basic (Declaration) | |
---|---|
Public Class CommandManager |
C# | |
---|---|
public class CommandManager |
C# | Copy Code |
---|---|
/* * Create an instance of CommandSimple and execute it. */ private void buttonExecute_Click(object sender, EventArgs e) { // Acquire a lock on the workbook set. workbookView.GetLock(); try { // Create a simple command. CommandSimple command = new CommandSimple(workbookView); // Execute the command. workbookView.ActiveCommandManager.Execute(command); } finally { // Release the lock on the workbook set. workbookView.ReleaseLock(); } } /* * Undo the last executed command if there is one. */ private void buttonUndo_Click(object sender, EventArgs e) { // Acquire a lock on the workbook set. workbookView.GetLock(); try { // Undo the last undoable command. workbookView.ActiveCommandManager.Undo(); } catch (Exception exc) { MessageBox.Show( this, exc.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Release the lock on the workbook set. workbookView.ReleaseLock(); } } /* * Demonstrate creating a simple undoable command which * toggles the display of gridlines. */ private class CommandSimple : SpreadsheetGear.Windows.Forms.Command { private WorkbookView _workbookView; private bool _saveDisplayGridlines; public CommandSimple(WorkbookView workbookView) : base(workbookView.ActiveWorkbook) { _workbookView = workbookView; } public override string DisplayText { get { // Text displayed in Undo menu. return "Toggle Display Gridlines"; } } public override CommandUndoSupport UndoSupport { get { // Undo is fully supported. return CommandUndoSupport.Full; } } protected override bool Execute() { // Verify that a lock has already been acquired. System.Diagnostics.Debug.Assert(_workbookView.ActiveWorkbookSet.HasLock); // Execute the command - we won't save state since we're // just toggling a boolean value. IWorksheetWindowInfo windowInfo = _workbookView.ActiveWorksheetWindowInfo; _saveDisplayGridlines = windowInfo.DisplayGridlines; windowInfo.DisplayGridlines = !_saveDisplayGridlines; return true; } protected override bool Undo() { // Undo the command. _workbookView.ActiveWorksheetWindowInfo.DisplayGridlines = _saveDisplayGridlines; return true; } } |
Each workbook set is associated with one instance of CommandManager. This association is established when the CommandManager constructor is called.
To create a custom undoable command:
- Create a custom command by subclassing Command or SpreadsheetGear.Windows.Forms.CommandRange.
- Override the Command.UndoSupport and / or CommandRange.UndoFlags properties.
- Override the Command.Undo method (not required with CommandRange which includes built-in support with CommandRange.UndoFlags).
- Override the Command.Execute method.
- Create and execute the command:
CommandCustom command = new CommandCustom();
workbookView1.ActiveCommandManager.Execute(command);
To override the behavior of an existing command:
- Create a new command manager by subclassing CommandManager.
- Override any of the methods which create default commands, such as CreateCommandPaste, and return the command you wish to execute. This can be an existing command with different options, a custom command with your own implementation, or null if you wish to disable the command.
- Instantiate your subclass of CommandManager which will make it the default command manager of the specified workbook set.
SpreadsheetGear.Windows.Forms.CommandManager
Target Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family