319341 HOW TO:在 Visual C# .NET 中處理 Windows 表單中 Office XP 工作表元件的事件 (From MKBA)

來源:互聯網
上載者:User
有關本文的 Microsoft Visual Basic .NET 版本,請參閱 319342.

本任務的內容
  • 概要
    • 分步指南
  • 參考

概要

您可以在 Visual C# .NET 中使用該分步指南,以便處理 Windows 表單中的 Office XP 工作表元件的事件。

返回頁首

分步指南

開始執行下列步驟之前,您必須修改 Visual Studio .NET 為 Office XP Web 元件 (OWC) 產生的類封裝。必須修改 Visual Basic .NET 的類封裝才能正確處理 OWC 事件。 有關其他資訊,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應的文章:

328275 HOW TO:Handle Events for the Office Web Components in Visual Studio .NET

  1. 建立一個新的 Visual C# .NET Windows 應用程式項目。將該項目命名為 SheetEvents。

    將建立 Form1,然後在“設計”視圖中開啟它。

  2. 在視圖菜單上,單擊工具箱。
  3. 將試算表組件從工具箱中拖放到 Form1。
  4. 在視圖菜單上,單擊代碼。
  5. 在 Form1.cs 檔案中的 NAMESPACE 語句前面添加下面幾行代碼:
    using System.Diagnostics;using OWC10 = Microsoft.Office.Interop.OWC;
  6. 將以下代碼添加到 EndEdit、BeforeContextMenu 和 CommandExecute 事件中:
    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. 按 F5 鍵產生並運行該樣本。
  8. 在儲存格 A1 中輸入一個值。

    如果該值不是 0 到 100 之間的數字,將會出現一條訊息並且編輯操作被取消。

  9. 按右鍵任一儲存格以顯示捷徑功能表,然後單擊菜單中的任一命令以便查看結果。

返回頁首

參考

有關其他資訊,請訪問下面的 Microsoft Web 網站:

使用 Visual Studio 進行 Microsoft Office 開發 http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficedev.asp

返回頁首 這篇文章中的資訊適用於:

  • Microsoft Office XP Web Components
  • Microsoft Visual C# .NET (2002)
最新動向: 2003-8-29 (1.0)
關鍵字: kbhowto kbHOWTOmaster kbOfficeWebSpread KB319341 kbAudDeveloper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.