319341 how to: process the event (from mkba) of the Office XP spreadsheet component in Windows Forms in Visual C #. net)

Source: Internet
Author: User
Tags knowledge base
For the Microsoft Visual Basic. Net version in this article, see 319342.

Content of this task
  • Summary
    • Step-by-step guide
  • Reference

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

  1. Create a New Visual C #. Net windows application project. Name the projectSheetevents.

    Create form1 and open it in the design view.

  2. InViewClickToolbox.
  3. SetWorkbooksDrag the component from the toolbox to form1.
  4. InViewClickCode.
  5. In the form1.cs fileNamespaceAdd the following lines of code before the statement:
    using System.Diagnostics;using OWC10 = Microsoft.Office.Interop.OWC;
  6. 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.}}
  7. PressF5Key Generation and run this example.
  8. 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.

  9. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.