Microsoft KB Archive/319341

= How To Handle Events for the Office XP Spreadsheet Component on a Windows Form in Visual C# .NET =

Article ID: 319341

Article Last Modified on 1/17/2007

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q319341



For a Microsoft Visual Basic .NET version of this article, see 319342.

IN THIS TASK
SUMMARY
 * Step-by-Step Guide

REFERENCES



SUMMARY
You can use this step-by-step guide with Visual C# .NET to handle events for an Office XP Spreadsheet component on a Windows Form.

back to the top

Step-by-Step Guide
Before you start the following steps, you must modify the class wrappers that Visual Studio .NET generates for the Office XP Web Components (OWC). You must modify the class wrappers for Visual Basic .NET to properly handle OWC events. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

328275 How To Handle Events for the Office Web Components in Visual Studio .NET

 Create a new Visual C# .NET Windows Application project. Name the project SheetEvents.

Form1 creates, and then opens in Design view. On the View menu, click Toolbox. Drag the Spreadsheet component from the Toolbox to Form1. On the View menu, click Code.  Add the following lines of code before the NAMESPACE statement in the Form1.cs file: using System.Diagnostics; using OWC10 = Microsoft.Office.Interop.OWC;   Add the following code to the EndEdit, BeforeContextMenu, and CommandExecute events: private void BeforeContextMenu(object sender, AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_BeforeContextMenuEvent e) { Debug.WriteLine(&quot;BeforeContextMenu Event: Create Custom Menu&quot;);

// Build the menu structure: // Menu Item       Submenu Item // ==============  ============    // - Format As... - Blue //                 - Red // - Enter Date object[] oAction1 = new object[]{&quot;&Blue&quot;,&quot;FormatAsBlue&quot;}; object[] oAction2 = new object[]{&quot;&Red&quot;, &quot;FormatAsRed&quot;}; object[] oAction3 = new Object[]{&quot;&Green&quot;,&quot;FormatAsGreen&quot;}; object[] oSubMenu1 = new object[]{oAction1, oAction2, oAction3}; object[] oMenu1 = new Object[]{&quot;&Format As...&quot;, oSubMenu1}; object[] oMenu2 = new object[]{&quot;&Enter Date&quot;, &quot;EnterDate&quot;}; object[] oMenu = new object[]{oMenu1, oMenu2}; e.menu.Value=oMenu;

}

private void CommandExecute(object sender, AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_CommandExecuteEvent e) { Debug.WriteLine(&quot;CommandExecute Event: Menu action = &quot; +           e.command.ToString);

OWC10.Range sel = axSpreadsheet1.Selection; object oColor = null;

// Take the action selected on the context menu. switch(e.command.ToString) {       case &quot;FormatAsRed&quot;: oColor = &quot;red&quot;; sel.Font.set_Color(ref oColor); break; case &quot;FormatAsBlue&quot;: oColor = &quot;blue&quot;; sel.Font.set_Color(ref oColor); break; case &quot;FormatAsGreen&quot;: oColor = &quot;green&quot;; sel.Font.set_Color(ref oColor); break; case &quot;EnterDate&quot;: sel.Formula=&quot;=TODAY&quot;; break; }

}

private void EndEdit(object sender, AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_EndEditEvent e) { Debug.Write(&quot;EndEdit Event: &quot;);

// Verify if the cell that is being edited is cell A1. object oOpt = System.Reflection.Missing.Value; string sAddr = axSpreadsheet1.ActiveCell.get_Address(       ref oOpt, ref oOpt, OWC10.XlReferenceStyle.xlA1, ref oOpt,                 ref oOpt); if(sAddr!=&quot;$A$1&quot;) {       Debug.WriteLine(&quot;Cell is Not A1, Allow edit&quot;); return; }

// If it is cell A1, confirm that the value entered is a number // between zero and 100. string sMsg = &quot;Cell A1 must contain a number between 0 and 100.&quot;; string sCaption = &quot;Spreadsheet10 Event Demo&quot;; try {       double dVal = System.Double.Parse(e.finalValue.Value.ToString); if((dVal<0)||(dVal>100)) {          // Value not between 0 and 100. Debug.WriteLine(                  &quot;Cell is A1 but value is not between 0 & 100. Cancel.&quot;); System.Windows.Forms.MessageBox.Show(sMsg, sCaption); e.cancel.Value=true; // Cancel the edit. }       else {          Debug.WriteLine(                   &quot;Cell is A1 and value is between 0 & 100. Allow edit.&quot;); }   }    catch (System.FormatException fe) {       // Cannot convert to a double. Debug.WriteLine(               &quot;Cell is A1 but the value is not a number. Cancel.&quot;); System.Windows.Forms.MessageBox.Show(sMsg, sCaption); e.cancel.Value=true;   // Cancel the edit. }

}

 Press F5 to build and to run the sample.</li> Enter a value in cell A1.

If the value is not a number between zero and 100, you receive a message, and then the edit cancels.</li> Right-click any cell to display the shortcut menu, and then click any of the commands on the menu so you can see the results.</li></ol>

back to the top

<div class="references_section">