SpreadsheetGear The Performance Spreadsheet Component Company 
CommandManager Class
See Also  Members   Example 
SpreadsheetGear.Windows.Forms Namespace : CommandManager Class
Provides the ability to override the behavior of commands executed by WorkbookView, RangeExplorer and WorkbookDesigner, as well as the ability to create new undoable commands.

Syntax

C# 
public class CommandManager 

Example

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; 
            } 
        } 
    

Remarks

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:

  1. Create a custom command by subclassing Command or CommandRange.
  2. Override the UndoSupport and / or UndoFlags properties.
  3. Override the Undo method (not required with CommandRange which includes built-in support with UndoFlags).
  4. Override the Execute method.
  5. Create and execute the command:

CommandCustom command = new CommandCustom();

workbookView1.ActiveCommandManager.Execute(command);

To override the behavior of an existing command:

  1. Create a new command manager by subclassing CommandManager.
  2. 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.
  3. Instantiate your subclass of CommandManager which will make it the default command manager of the specified workbook set.

Inheritance Hierarchy

System.Object
   SpreadsheetGear.Windows.Forms.CommandManager

Requirements

Namespace: SpreadsheetGear.Windows.Forms

Platforms: Windows Vista, Windows XP, Windows Server 2008, Windows Server 2003, Windows 2000, Windows Me and Windows 98, including 32 bit and 64 bit editions where applicable. SpreadsheetGear for .NET 1.x requires the Microsoft .NET Framework 1.1 or .NET 2.0 (works with .NET 3.x). SpreadsheetGear for .NET 2007 requires the Microsoft .NET Framework 2.0 (works with .NET 3.x).

Assembly: SpreadsheetGear (in SpreadsheetGear.dll)

See Also