'SheetChange event is not getting changes of active excel cell when performing another function

I am coding simple application having one window form using c# in visual studio, app exe will be opened by excel VBA macro and the application will read the value of activeworkbook.activesheet.cell(1,1) at startup and when any change occur to active sheet.

the form have the following controls:

  1. label control name (label1)
  2. ZEDGraphpan name (zedGraphControl1)

In c# code there are two commands

  • ReadExcel();
  • PlotGraph();

Readexcel: to update label1 text equal to ActiveWorkbook.ActiveSheet.cell(1,1).value on startup and on any change event in activesheet

PlotGraph: to generate multiple curves in ZEDGRAPH

The Issue:

  • If the code have Readexcel() alone it is working fine and updating label.text at start up and at every change of active sheet.
  • If code include both of Readexcel() and PlotGraph() together the application will update label1.text only at startup, it will not pick any change in activesheet hence not updating label1 in real-time.

The Question: how can I get the Readexcel() to pick the live changes in activesheet along with PlotGrah in the same code

the Code of the App is below

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;
using ZedGraph;
using xl = Microsoft.Office.Interop.Excel;


namespace TestLiveZed
{
    public partial class GraphForm : Form
    {
        public object ZedGraphcontrol1 { get; private set; }
        
        
        public GraphForm()
        {
            InitializeComponent();

            PlotGraph();
            
            ReadExcel();
            
        }
        

        private void OWB_SheetChange(Object Sh, Range Target)
        {
            UpdateText(Target.Value.ToString());
        }
        
        private delegate void UpdateProgDelegate(String uptxt);
        private void UpdateText(String uptxt)
        {
            if (this.label1.InvokeRequired)
            {
                UpdateProgDelegate updateCallBack = new UpdateProgDelegate(UpdateText);
                this.Invoke(updateCallBack, new object[] { uptxt });
            }
            else
            {
                label1.Text = uptxt.ToString();
            }
            
        }
        private void ReadExcel()
        {
            xl.Application oXL = (xl.Application)Marshal.GetActiveObject("Excel.Application");
            Workbook oWB;
            Worksheet oSheet;
            
            oWB = oXL.ActiveWorkbook;
            oSheet = oWB.ActiveSheet;
            oWB.SheetChange += OWB_SheetChange;
            label1.Text = Convert.ToString(oSheet.Cells[1, 1].Value);
            
        }

        
        private void PlotGraph()
        {
            GraphPane myPane = zedGraphControl1.GraphPane;
            myPane.Title.Text = "Company A VS B";
            myPane.XAxis.Title.Text = "r/D";
            myPane.YAxis.Title.Text = "K";

            
            string[] clr = { "Black", "Green", "Blue", "Orange", "Yellow", "Black", "Cyan", "DarkCyan", "Aqua", "Purple", "Pink", "Red", "AliceBlue", "Grey", "DarkGreen" };
            int tr = 0;

            for (int y = 0; y <= 180; y = y+15)
            {
            
                AddCurv(y,tr, clr);
                
                tr++;
                
            }
        
        }

        private void AddCurv(int y,int tr,string[] clr)
        {
            PointPairList CompAList = new PointPairList();
            for (double i = 0.5; i < 16; i = i + 0.2)
            {
                double f = 0.02;
                int Beta = y;
                double Rratio = i;
                double C = Math.Sin(0.5 * Beta * Math.PI / 180);
                double AL = Beta * Math.PI / 180;
                double r = (Math.Pow(C, 0.5) + C) / Math.Pow(Rratio, 4 * AL / Math.PI);
                double K = f * AL * Rratio + (0.10 + 2.4 * f) * C + (6.6 * f * r);


                CompAList.Add(i, K);
                //CompBList.Add(i + 1, ComBData[i]);
                
            };
            string vclr = clr[tr];
            _ = zedGraphControl1.GraphPane.AddCurve("Angle = " + y.ToString(), CompAList, Color.FromName(vclr), SymbolType.None);
            
            
            zedGraphControl1.GraphPane.YAxis.Scale.Min = 0;
            zedGraphControl1.GraphPane.YAxis.Scale.Max = 1;
            zedGraphControl1.GraphPane.XAxis.Scale.Min = 0;
            zedGraphControl1.GraphPane.XAxis.Scale.Max = 16;
            zedGraphControl1.AxisChange();
            zedGraphControl1.Refresh();
            
        }
    }
}



Solution 1:[1]

Finally I managed to make it works

Set the form events on activated and on deactivated to run the void Readexcel();

private void Form1_Activated(object sender, EventArgs e)
    {
        ReadExcel();
    }

private void Form1_Deactivate(object sender, EventArgs e)
    {
        ReadExcel();
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1