프로그래밍

C# 델리게이트를 이용한 다이얼로그 간 통신 구현 소스

준이바라기얍 2021. 8. 9. 15:35
반응형
clsEventBridge.DataListSend += new clsEventBridge.DataListSendEventHandler(DataListSet);
            clsEventBridge.MSGNameSend += new clsEventBridge.MSGNameSendEventHandler(MSGNameSet);

부모 자식 창 사이 데이터를 주고 받을 일이 종종 있습니다.

 

다양한 방법이 있지만 전 델리게이터를 사용하는 방법을 선호합니다.

 

A창에서 이벤트를 발생 시키고 B창에서 캐치하는 식으로요.

 

아래와 같이 쓰면 됩니다.

 

간단하니 이부분도 설명은 패스하는 걸로...

class clsEventBridge
    {
        /// <summary>
        /// dataList Send
        /// </summary>
        /// <param name="DataList"></param>
        public delegate void DataListSendEventHandler(Dictionary<string, string> DataList);
        public static event DataListSendEventHandler DataListSend;

        public static void DataListEventSend(Dictionary<string, string> DataList)
        {
            DataListSend(DataList);
        }

        /// <summary>
        /// Msg Edit
        /// </summary>
        /// <param name="SystemMSG"></param>
        public delegate void MSGNameSendEventHandler(string MSGName, bool AutoChk);
        public static event MSGNameSendEventHandler MSGNameSend;

        public static void MSGNameEventSend(string MSGName, bool AutoChk)
        {
            MSGNameSend(MSGName, AutoChk);
        }


    }
private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dtlist = new Dictionary<string, string>();

                for (int i = 0; i < dtDataList.Rows.Count; i++)
                {
                    if (dtDataList.Rows[i].Cells[0].Value != null)
                        dtlist.Add(dtDataList.Rows[i].Cells[0].Value.ToString(), dtDataList.Rows[i].Cells[1].Value.ToString());
                }
                clsEventBridge.DataListEventSend(dtlist);
            }
            catch (Exception ex)
            {
                clsLOG.SetLOG(LOGTYPE.SYSTEMERROR, ex.ToString());
            }
            this.Close();
        }
void DataListSet(Dictionary<string, string> DataList)
        {
            try
            {
                CurrNode.Nodes.Clear();

                foreach (KeyValuePair<string, string> dt in DataList)
                {
                    CurrNode.Nodes.Add(dt.Key + "=" + dt.Value);
                }

                CurrNode.Expand();

            }
            catch (Exception ex)
            {
                clsLOG.SetLOG(LOGTYPE.SYSTEMERROR, ex.ToString());
            }
        }
반응형