SpreadsheetGear
CommandManager Class
Members  Example  See Also  Send Feedback
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

Visual Basic (Declaration) 
Public Class CommandManager 
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 Command.UndoSupport and / or CommandRange.UndoFlags properties.
  3. Override the Command.Undo method (not required with CommandRange which includes built-in support with CommandRange.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.

Inheritance Hierarchy

System.Object
   SpreadsheetGear.Windows.Forms.CommandManager

Requirements

Namespace: SpreadsheetGear.Windows.Forms

Platforms: x86 and x64 versions of Windows 2000, Windows XP, Windows Vista, Windows 7, Windows Server 2003 (including R2) and Windows Server 2008 (including R2). SpreadsheetGear 2009 requires the Microsoft .NET Framework 2.0 and supports .NET 3.0 and .NET 3.5.

Assembly: SpreadsheetGear (in SpreadsheetGear.dll)

See Also

Copyright © 2003-2009 SpreadsheetGear LLC. All Rights Reserved.Help Powered by Innovasys   
SpreadsheetGear is a registered trademark of SpreadsheetGear LLC.
Microsoft, Microsoft Excel and Visual Studio are trademarks or registered trademarks of Microsoft Corporation.