locked
Automation - Creating A Chart in Excel Spreadsheet - getChartElement method not working RRS feed

  • Question

  • User1014951736 posted

     Here is my code, pretty simple. I'm creating a an excel chart with C# and assigning a new mouseup event. I have tested the event and it fires correctly on the created chart giving me the x and y coordinates relative to the entire chart sheet. However I want to call the getChartElement method inside the click event to find which chart element was clicked on so I cant get the x and y values relative to the chart itself. However the method doesn't seem to be returning a value into the ElementID, Arg1, and Arg2 parameters that I am passing in as references.

    Excel.Application xlApp;
    Excel._Workbook wBook;
    
    void createChart(string xlfilePath)
    {
    
         xlApp = new Excel.Application();
    
         wBook = xlApp.Workbooks.Open(xlfilePath,
              0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
              true, false, 0, true, false, false);
    
         Excel.Chart ratingChart = (Excel.Chart)wBook.Charts.Add(Missing.Value,
                                              Missing.Value, Missing.Value, Missing.Value);
    
         ratingChart.ChartType = Excel.XlChartType.xlXYScatter;
    
         ratingChart.MouseUp += new
                   Microsoft.Office.Interop.Excel.ChartEvents_MouseUpEventHandler
                   (ratingChart_MouseUp);
    
    }
    
    
    // Mouse up event
    void ratingChart_MouseUp(int Button, int Shift, int x, int y)
    {
        int ElementID = 3; //Set to Arbitrary values to see if they change during runtime
        int Arg1 = 3;
        int Arg1 = 3;
    
       Excel.Chart currentChart = (Excel.Chart)wBook.ActiveChart;
    
       currentChart.GetChartElement(x, y, ref ElementID, ref Arg1, ref Arg2);
    
    
    }

    When running the debugger, the values of ElementID, Arg1, and Arg2 never change in value even after the getChartElement has been excecuted.

    Any help with this would be appreciated.

    Saturday, June 27, 2009 2:08 PM

All replies

  • User-675654366 posted

    Bumping this thread, as I'm having the same issue (but in VB.NET). Does anyone out there have a solution?

    Wednesday, September 9, 2009 5:14 PM