有關本文的 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
- 建立一個新的 Visual C# .NET Windows 應用程式項目。將該項目命名為 SheetEvents。
將建立 Form1,然後在“設計”視圖中開啟它。
- 在視圖菜單上,單擊工具箱。
- 將試算表組件從工具箱中拖放到 Form1。
- 在視圖菜單上,單擊代碼。
- 在 Form1.cs 檔案中的 NAMESPACE 語句前面添加下面幾行代碼:
using System.Diagnostics;using OWC10 = Microsoft.Office.Interop.OWC;
- 將以下代碼添加到 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.}}
- 按 F5 鍵產生並運行該樣本。
- 在儲存格 A1 中輸入一個值。
如果該值不是 0 到 100 之間的數字,將會出現一條訊息並且編輯操作被取消。
- 按右鍵任一儲存格以顯示捷徑功能表,然後單擊菜單中的任一命令以便查看結果。
返回頁首
參考
有關其他資訊,請訪問下面的 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 |