Excel选择更改事件触发两次C#
我试图在excel中调用selectionchange事件,但它触发了两次。 我通过编组连接到一个打开的Excel文件,因此代码如下 首先,我将代码发布在Form1的Excel选择更改事件触发两次C#,c#,excel,event-handling,marshalling,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 Sys
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;
}
}
}
}
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());
}
}
下面是ExcelSinifveOlaylar类
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;
}
}
}
}
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());
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Excel=Microsoft.Office.Interop.Excel;
使用System.Windows.Forms;
运用系统反思;
使用系统线程;
名称空间Excel\u Sol\u Taraf\u Onaylama\u V.\u 00
{
卓越类
{
//奈斯内勒的巴兰特·伊斯ı伊ç编组
Excel.applicationoxl;
Excel._工作簿oWB;
Excel.oSheet工作表;
//Excel事件委托变量:
Excel.AppEvents\u Workbook BeforeCloseEventHandler EventDel\u BeforeBookClose//
Excel.DocEvents\u ChangeEventHandler EventDel\u cellshange;
Excel.DocEvents\u SelectionChangeEventHandler EventDel\u SelChange;
int eventtrigger=0;
公共Excel.\u应用程序oXL1(字符串strDosyaAdi)
{
oXL=(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject(“Excel.Application”);
intFileNr=2;
//此for语句部分仅用于连接到test.xls
对于(int i=1;i能否请您尝试一个单元格更改事件
事件?请直接尝试以下代码,而不是在定义事件
中创建事件
//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);
}
只需移除
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
{
//because of this its occuring 2 times as its already registered from class constructor //myExcel.Excel_OlaylariTanimla();
MessageBox.Show("Excel Olayları Tanımlandı");
}
catch (Exception)
{
throw;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
}
你的for
循环在这里有什么用?不管for
,你在void Excel\u OlaylariTanimla()中不是有两个eventDel\u cellshange
??使用for循环,我将连接所需的excel工作簿。也许最好将其他类的代码提供给。如果您想查看,我还将发布其他类(Form1类)调用方法的地方。@macrobook我更新了代码。您的代码流没有说服力。请您尝试一下这个示例。然后逐段添加您的其他额外代码段,尝试catch等等。如果成功,我将尝试并共享其余的代码。由于它是代码的一部分,可能最好提供整个代码。您也可以尝试。Trigg将事件重复两次不会对代码操作产生负面影响,但我对原因持肯定态度。我的问题是关于单元格选择的更改,因此,我将提供这两个类,以便您也可以尝试代码。非常感谢。@macrobook事件在您仅更改一次单元格时不会触发多次。因此这是一个问题r进行检查。除非有两个事件用于单元格选择
和单元格更改
。。。