For the Microsoft Visual Basic. Net version in this article, see 319342.
Content of this task
Summary
You can use this step-by-step guide in Visual C #. Net to process events of the Office XP spreadsheet component in Windows Forms.
Back to Top
Step-by-step guide
Before you begin to perform the following steps, you must modify Visual Studio. NET to the class packaging generated by the Office XP Web component (owc. You must modify the class packaging of Visual Basic. Net to correctly handle the owc event. For other information, click the following article number 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 projectSheetevents.
Create form1 and open it in the design view.
- InViewClickToolbox.
- SetWorkbooksDrag the component from the toolbox to form1.
- InViewClickCode.
- In the form1.cs fileNamespaceAdd the following lines of code before the statement:
using System.Diagnostics;using OWC10 = Microsoft.Office.Interop.OWC;
- Add the following codeEndedit,BeforecontextmenuAndCommandexecuteEvent:
private void BeforeContextMenu(object sender, AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_BeforeContextMenuEvent e){Debug.WriteLine("BeforeContextMenu Event: Create Custom Menu");// Build the menu structure:// Menu Item Submenu Item// ============== ============// - Format As... - Blue// - Red// - Enter Dateobject[] oAction1 = new object[]{"&Blue","FormatAsBlue"};object[] oAction2 = new object[]{"&Red", "FormatAsRed"};object[] oAction3 = new Object[]{"&Green","FormatAsGreen"};object[] oSubMenu1 = new object[]{oAction1, oAction2, oAction3};object[] oMenu1 = new Object[]{"&Format As...", oSubMenu1};object[] oMenu2 = new object[]{"&Enter Date", "EnterDate"};object[] oMenu = new object[]{oMenu1, oMenu2};e.menu.Value=oMenu;}private void CommandExecute(object sender, AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_CommandExecuteEvent e){Debug.WriteLine("CommandExecute Event: Menu action = " + e.command.ToString());OWC10.Range sel = axSpreadsheet1.Selection;object oColor = null;// Take the action selected on the context menu.switch(e.command.ToString()){case "FormatAsRed":oColor = "red";sel.Font.set_Color(ref oColor);break;case "FormatAsBlue":oColor = "blue";sel.Font.set_Color(ref oColor);break;case "FormatAsGreen":oColor = "green";sel.Font.set_Color(ref oColor);break;case "EnterDate":sel.Formula="=TODAY()";break;}}private void EndEdit(object sender, AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_EndEditEvent e){Debug.Write("EndEdit Event: ");// 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!="$A$1"){Debug.WriteLine("Cell is Not A1, Allow edit");return;}// If it is cell A1, confirm that the value entered is a number // between zero and 100.string sMsg = "Cell A1 must contain a number between 0 and 100.";string sCaption = "Spreadsheet10 Event Demo";try{double dVal = System.Double.Parse(e.finalValue.Value.ToString());if((dVal<0)||(dVal>100)){ // Value not between 0 and 100. Debug.WriteLine( "Cell is A1 but value is not between 0 & 100. Cancel."); System.Windows.Forms.MessageBox.Show(sMsg, sCaption); e.cancel.Value=true;// Cancel the edit.}else{ Debug.WriteLine( "Cell is A1 and value is between 0 & 100. Allow edit.");}}catch (System.FormatException fe){// Cannot convert to a double.Debug.WriteLine( "Cell is A1 but the value is not a number. Cancel.");System.Windows.Forms.MessageBox.Show(sMsg, sCaption);e.cancel.Value=true;// Cancel the edit.}}
- PressF5Key Generation and run this example.
- Enter a value in cell A1.
If the value is not a number between 0 and 100, a message is displayed and the edit operation is canceled.
- Right-click any cell to display the shortcut menu, and click any command in the menu to view the result.
Back to Top
Reference
For more information, visit the following Microsoft Web site:
Http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficedev.asp for Microsoft Office Development with Visual Studio
The information in this article is applicable:
- Microsoft Office XP Web Components
- Microsoft Visual C #. Net (2002)
Latest updates: |
(1.0) |
Keywords: |
Kbhowto kbhowtomaster kbofficewebspread kb319341 kbauddeveloper |