319341 HOW TO:在 Visual C# .NET 中处理 Windows 窗体中 Office XP 电子表格组件的事件 (From MKBA)

有关本文的 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. 将以下代码添加到 EndEditBeforeContextMenuCommandExecute 事件中:
    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 Date
    	object[] 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值