天天看點

主窗體群發子窗體,主窗體接收子窗體資訊窗體結構

窗體結構

主窗體群發子窗體,主窗體接收子窗體資訊窗體結構

第一,主窗體項目

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace ReceivemasgDelegate

{

    public partial class mainfrm : Form

    {

        /// <summary>

        /// 系統無參構造函數

        /// </summary>

        public mainfrm()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 主窗體群發委托

        /// </summary>

        public Action<string, string> sendmsg = null; //建立委托變量sendmsg

        /// <summary>

        /// 子窗體集合

        /// </summary>

        private List<childfrm> OtherForms = new List<childfrm>();

        /// <summary>

        /// 聲明子窗體執行個體

        /// </summary>

        public childfrm otherfrm = null;

        /// <summary>

        /// 主窗體收消息方法

        /// </summary>

        /// <param name="msg"></param>

        /// <param name="frmname"></param>

        private void ReceviedMsg(string msg, string frmname)

        {

            this.tbReceiveMsg.Text += "\r\n 來自:" + frmname + "消息:" + msg;

        }

        /// <summary>

        /// 建立子窗體按鈕事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnaddfrm_Click(object sender, EventArgs e)

        {

            for (int i = 1; i < 5; i++)

            {

                otherfrm = new childfrm("子窗體:" + i);

                sendmsg = otherfrm.ReceviceFrmMsg;

                otherfrm.Show();

                OtherForms.Add(otherfrm);

                otherfrm.RecevieMsgDelegate = ReceviedMsg;

            }

        }

        /// <summary>

        /// 主窗體群發消息按鈕事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnsendMsg_Click(object sender, EventArgs e)

        {

            foreach (var form in OtherForms)

            {

                sendmsg = form.ReceviceFrmMsg;                               //foreach 循環在子窗體集合中取出每個窗體,并調用子窗體的接受方法

                this.sendmsg(this.Text.Trim(), this.tBSendMsg.Text.Trim());

            }

        }

    }

}

第二,子窗體項目

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace ReceivemasgDelegate

{

    public partial class childfrm : Form

    {

        /// <summary>

        /// 系統無參構造函數

        /// </summary>

        public childfrm()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 系統委托

        /// </summary>

       public  Action<string, string>  RecevieMsgDelegate=null;  //聲明系統委托變量RecevieMsgDelegate

        /// <summary>

        /// 有參構造函數

        /// </summary>

        /// <param name="frmname">子窗體名稱參數</param>

        public childfrm(string frmname )

        {          

            InitializeComponent();

            this.Text = frmname;            

        }

      /// <summary>

      /// 委托方法

      /// </summary>

      /// <param name="mainfrmname"></param>

      /// <param name="mainfrmmsg"></param>

        public  void ReceviceFrmMsg(string mainfrmname ,string mainfrmmsg)

        {

            this.textBox2.Text += "\r\n 來自:"+mainfrmname+ "廣播消息:"+mainfrmmsg;

        }

        /// <summary>

        /// 發送消息事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnsendMsg_Click(object sender, EventArgs e)

        {

           RecevieMsgDelegate(this.textBox2.Text.Trim(), this.Text.Trim());   

        }

    }

}

c#