c# - Excel 选择更改事件触发了两次 C#

标签 c# excel event-handling marshalling

我尝试在 excel 中调用 selectionchange 事件,但它触发了两次。
我用编码连接到一个打开的Excel文件,所以代码如下

首先,我在 Form1 上发布代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;

namespace Excel_Sol_Taraf_Onaylama_V._00
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }



        private void btn_Excele_Baglan_Classtan_Click(object sender, EventArgs e)
        {
            try
            {

                ExcelSinifveOlaylar myExcel = new ExcelSinifveOlaylar();

                Excel.Application oXL = (Excel.Application)myExcel.oXL1("Test.xlsx");

                try
                {
                    myExcel.Excel_OlaylariTanimla();
                    MessageBox.Show("Excel Olayları Tanımlandı");
                }
                catch (Exception)
                {

                    throw;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
        }
    }
}

所以下面是 ExcelSinifveOlaylar 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace Excel_Sol_Taraf_Onaylama_V._00
{


    class ExcelSinifveOlaylar
    {

        //Marshalling ile excel bağlantısı için nesneler
        Excel.Application oXL;
        Excel._Workbook oWB;
        Excel.Worksheet oSheet;

        //Excel event delegate variables:
        Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose; //
        Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;
        Excel.DocEvents_SelectionChangeEventHandler EventDel_SelChange;
        int eventtrigger = 0;
        public Excel._Application oXL1(string strDosyaAdi)
        {
            oXL = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

            int intFileNr = 2;
            //This for statement part is only to connect to the test.xls
            for (int i = 1; i <= 3; i++)
            {
                intFileNr = i;
                try
                {
                    oWB = (Excel._Workbook)oXL.Workbooks.get_Item(i);
                    if (strDosyaAdi == oWB.Name)
                    {
                        oSheet = (Excel.Worksheet)oWB.ActiveSheet;
                        MessageBox.Show("Connected to " + strDosyaAdi + "Aktif Sayfa: " + oSheet.Name.ToString());
                    }

                    Excel_OlaylariTanimla();
                    break;
                }
                catch
                {
                    intFileNr = i;
                }
            }
            return oXL;
        }
        public void Excel_OlaylariTanimla()
        {
            try
            {
                EventDel_SelChange = new Excel.DocEvents_SelectionChangeEventHandler(SelChange);
                oSheet.SelectionChange += EventDel_SelChange;
               // EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(CellsChange);
               // oSheet.Change += EventDel_CellsChange;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void SelChange(Excel.Range Target)
        {

            //MessageBox.Show("Selection Changed");
            eventtrigger++;
            MessageBox.Show(eventtrigger.ToString());
        }
}

当我在 excel 中更改所选单元格时,selectionchange 事件触发了两次。因此事件触发器变为 2 而不是 1。

我觉得这两个类中的以下声明可能会导致这个问题

使用 Excel = Microsoft.Office.Interop.Excel;

但我不确定。
谢谢您的支持。

最佳答案

你能试试吗cell change事件?直接尝试以下代码,而不是在 define events 中创建事件.

//Add an event handler for the Change event of both worksheet objects.
   EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler( CellsChange);

   xlSheet1.Change += EventDel_CellsChange;

private void CellsChange(Excel.Range Target )
{
   //This is called when any cell on a worksheet is changed.
   Debug.WriteLine("Delegate: You Changed Cells " + 
      Target.get_Address( Missing.Value, Missing.Value, 
      Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value ) + 
      " on " + Target.Worksheet.Name);
}

Reference MSND:Handle Events for Excel by Using Visual C# .NET

关于c# - Excel 选择更改事件触发了两次 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13947738/

相关文章:

vba - 将功能键分配给 excel 宏

c# - 如何使用 Caliburn Micro 在 Shell 的不同内容控件中加载控件

c# - 使用 Place API 在 xamarin android 中填充附近地点的 map

c# - 如何根据 SQL 语法在给定元数据信息和解析树的情况下确定 SQL 语句中的列属于哪个表?

java - POI API 的事件模型在 3.15 版本中不适用(出现编译错误)吗?

vb.net - 打开共享 LostFocus 事件处理程序的发送器

c# - 将委托(delegate)传递给 Sort()

python - CSV 文件创建问题

python - 在 Pygame 中使 Sprite 跳跃

java - 在定时器开始触发后重新设置延迟