C#中在定義事件委託時怎樣跨窗體傳遞參數

場景

C#中委託與事件的使用-以Winform中跨窗體傳值爲例:編程

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100150700this

參照上文在定義事件時傳遞參數爲簡單的string,若是要傳遞比較複雜的參數,那麼就可使用對象將參數進行封裝。spa

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。.net

實現

在窗體A中定義委託與事件code

public delegate void RefreshChartInOneDelegete(XYModel xYModel);
public static event RefreshChartInOneDelegete OnRefreshChartInOne;

 

其中XYModel就是本身封裝的參數的Model,具體能夠根據須要本身封裝。orm

 

public class XYModel
    {
        //存儲X軸屬性
        private XAxisModel xAxisModel;
        //存儲Y軸屬性
        private List<YAxisModel> yAxisModelList;

        
        public XAxisModel XAxisModel
        {
            get { return xAxisModel; }
            set { xAxisModel = value; }
        }

        public List<YAxisModel> YAxisModelList
        {
            get { return yAxisModelList; }
            set { yAxisModelList = value; }
        }
    }

 

定義觸發器對象

 xYModel.YAxisModelList = yAxisModelList;
            if (OnRefreshChartInOne != null)
            {
                OnRefreshChartInOne(xYModel);
                
            }

 

而後在窗體B中進行事件訂閱blog

FrmChartOptionInOneCurCom.OnRefreshChartInOne += new FrmChartOptionInOneCurCom.RefreshChartInOneDelegete(ChartCompareHelper_OnRefreshChart);

 

在窗體B中編寫具體的實現教程

 private void ChartCompareHelper_OnRefreshChart(XYModel xYModel)
        {
            xYModelStore = xYModel;
            ChartCompareHelper.RefreshPaneComInOne(this.zedGraphControl1,xYModel.YAxisModelList);
        }
相關文章
相關標籤/搜索