C# implements value transfer between multiple windows through delegation

When I used Qt to write before, I had signals and slots to implement it. If I use C#, there should be something similar.

What I probably achieved is that I created two new child windows in the parent window. Both child windows can pass the processed data to the parent window, and the two child windows communicate through the parent window.

I will explain the code according to the window name above.

Account and password login window


        public delegate void SendToken(string token);
        public event SendToken sendToken;
        private void btnLogin_Click(object sender, EventArgs e)
        {

           ....

                sendToken(token);

        }

Parent window


        public delegate void SendMerchantFormToken(string msg);   // 定义转发消息的委托
        public event SendMerchantFormToken sendMerchantFormToken;   // 定义事件



        public MerchantForm merchantForm;
        public LoginForm loginForm;

        //打开登录界面
        void openNewForm()
        {
            loginForm = new LoginForm();
            loginForm.BringToFront();
            loginForm.MaximizeBox = false;//最大化
          
            loginForm.Text = "账号密码登录";
        
            loginForm.sendToken += new LoginForm.SendToken(receiveloginFormToken);  // 订阅子窗体1的send事件
            loginForm.TopMost = true;//置于顶层
            loginForm.StartPosition = FormStartPosition.CenterScreen;
            loginForm.Show();
          
            merchantForm = new MerchantForm();
            merchantForm.BringToFront();
            merchantForm.MaximizeBox = false;//最大化
          
            merchantForm.Text = "商户选择";
            merchantForm.TopMost = true;//置于顶层
            merchantForm.StartPosition = FormStartPosition.CenterScreen;
            merchantForm.Hide();

            sendMerchantFormToken += new SendMerchantFormToken(merchantForm.getForm1Token);

        }

        //是委托代理的方法,也就是要最终执行的方法,需要定义并实现
        private void receiveloginFormToken(string msg)
        {
           
            sendMerchantFormToken(msg);
            merchantForm.Show();
        }

Merchant list window


        public void getForm1Token(string str)
        {

             MessageBox.Show(str);
            



        }

Guess you like

Origin blog.csdn.net/title71/article/details/131303731