SpreadsheetGear 2012
CommandManager Class
Members  Example 






Provides the ability to override the behavior of commands , as well as the ability to create new undoable commands.
Object Model
CommandManager Class
Syntax
'Declaration
 
Public Class CommandManager 
'Usage
 
Dim instance As CommandManager
public class CommandManager 
public class CommandManager 
public class CommandManager 
public __gc class CommandManager 
public ref class CommandManager 
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 Command.UndoSupport and / or UndoFlags properties.
  3. Override the Command.Undo method (not required with CommandRange which includes built-in support with UndoFlags).
  4. Override the Command.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.
Example
/*
         * 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.Commands.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.
                SG.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;
            }
        }
Inheritance Hierarchy

System.Object
   SpreadsheetGear.Commands.CommandManager

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

CommandManager Members
SpreadsheetGear.Commands Namespace
Command Class
CommandRange Class

Send Feedback