2012-12-19 87 views
2

我嘗試在Excel中調用selectionchange事件,但它觸發兩次。 我連接到同編組的打開Excel文件,因此代碼如下Excel選擇更改事件觸發兩次C#

首先我發佈的代碼在爲Form

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事件觸發了兩次。所以eventtrigger變爲2,而不是上1.

我已經感覺也許在這兩個類下面聲明導致使用Excel =的Microsoft.Office.Interop.Excel此問題

;

但我不確定。 感謝您的支持。

+0

有什麼用你的'這裏for'循環?不管'for',不要在'void Excel_OlaylariTanimla()'中有兩個'eventDel_cellschange'? – bonCodigo

+0

使用for循環,我將連接所需的excel工作簿。也許最好是提供其他類的代碼。如果你想看到它,我還會發布調用方法的其他類(Form1類)。 – macrobook

回答

0

你可以請嘗試一個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

+0

我嘗試過,但相同。 – macrobook

+0

@macrobook我更新了代碼。您的代碼流並不令人信服。你可以試試這個樣品嗎?然後逐個添加其他額外片段,嘗試抓取等。 – bonCodigo

+0

如果我成功了,我會嘗試和其他人分享。由於它是代碼的一部分,所以提供整體可能會更好。而且你也可以試試。 – macrobook

0

只是刪除

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; 
     } 
    }