利用委托 实现窗体间通信,非原创

copy自:https://www.cnblogs.com/hugoNB/p/7130562.html,这个作者写的浅显易懂,就复制下来自己看

实现过程:

    这里主要是用到委托实现,所以主要描述一下委托在这里的应用。

    我们要在主窗体(这里的子父窗体都是自己假想)中获取子窗体中的元素,所以首先在主窗体声明一个委托,这个委托是用来干嘛的?我们知道,在子窗体勾选几个选项点击确定之后,在这个事件中,我们需要将勾选的内容传送到主窗体,这里的委托的含义就是:我主窗体有给TextBox显示文本的方法,但是我主窗体干不了这件事儿,因为我没有文本,文本在你子窗体那里,所以主窗体得委托子窗体干一件事儿,这个事儿就是麻烦你子窗体把勾选的东西的文本给我显示到主窗体传的TextBox中。

//1、声明一个委托
public delegate void showText(List<String> ls);

    声明完委托后,在子窗体(Form2/Form3)实例化一个委托,这个委托才是真真正正的委托,是干事的委托。

//2、实例化一个委托
public showText f2;

     那有了委托之后,你子窗体需要干什么事情呢?来,就是干这件事儿:麻烦你帮我把list集合中的字符串显示到textBox1里面去。该方法是在主窗体中写的。

复制代码
 //3、委托的方法,这个方法应该和第一步是同步实现的,这里暂且记作第3步。   
private void T1Show(List<String> ls) { stringList1 = ls; stringList1.Sort(); this.textBox1.Text = null; foreach (String item in stringList1) { this.textBox1.Text += item + ";"; } }
复制代码

     委托子窗体要干的事情有了,接下来就是把这件事委托给子窗体。

//4、把要干的事情委托给子窗体已经创建好的委托载体f2.
objForm.f2 = this.T1Show;

    到这里基本上就实现了子父窗体利用委托进行窗体间通信,先把整个项目的代码展示出来:

主窗体代码:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace 云状
{
    public partial class Form1 : Form
    {
        //保存当前已经添加到数据库的气象代码
       // public List<String> stringList1 = new List<string>();
        //private List<String> stringList2 = new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            Form2 objForm = new Form2();
            objForm.FormBorderStyle = FormBorderStyle.None;
            //4、把要干的事情委托给子窗体已经创建好的委托载体f2.
            objForm.f2 = this.T1Show;
            objForm.ShowDialog();

        }
        //3、委托的方法,这个方法应该和第一步是同步实现的,这里暂且记作第3步。
        private void T1Show(List<String> ls)
        {
            stringList1 = ls;
            stringList1.Sort();
            this.textBox1.Text = null;
            foreach (String item in stringList1)
            {
                this.textBox1.Text += item + ";";
            }
        }
        private void T2Show(List<String> ls)
        {
            stringList2 = ls;
            stringList2.Sort();
            this.textBox2.Text = null;
            foreach (String item in stringList2)
            {
                this.textBox2.Text += item + ";";
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            
            Form3 objForm = new Form3();
            objForm.FormBorderStyle = FormBorderStyle.None;
            objForm.f3 = this.T2Show;
            objForm.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //入库
        }
    }
    //1、声明一个委托
    public delegate void showText(List<String> ls);
}
复制代码

子窗体Form2代码:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace 云状
{
    public partial class Form2 : Form
    {
        //2、实例化一个委托
        public showText f2;
        public Form2()
        {
            InitializeComponent();
            if (Form1.stringList1 != null)
            {
                foreach (Control item in this.panel1.Controls)
                {
                    if (item is CheckBox)
                    {
                        string str = ((CheckBox)item).Text.Substring(0, 2);
                        if (Form1.stringList1.Contains(str))
                        {
                            ((CheckBox)item).Checked = true;
                        }
                    }
                }                 
            }
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<String> ls=new List<string>();
            foreach(Control item in this.panel1.Controls)
            {
                if(item is CheckBox)
                {
                    if (((CheckBox)item).Checked==true)
                    {
                        ls.Add(((CheckBox)item).Text.Substring(0, 2));
                    }
                }
            }
            if(f2!=null)
            {
                f2(ls);
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
复制代码

子窗体Form3代码:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 云状
{
    public partial class Form3 : Form
    {
        public showText f3;
        public Form3()
        {
            InitializeComponent();
            if (Form1.stringList2 != null)
            {
                foreach (Control item in this.panel1.Controls)
                {
                    if (item is CheckBox)
                    {
                        string str = ((CheckBox)item).Text.Substring(0, 2);
                        if (Form1.stringList2.Contains(str))
                        {
                            ((CheckBox)item).Checked = true;
                        }
                    }
                }                
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
                List<String> ls=new List<string>();
                foreach (Control item in this.panel1.Controls)
                {
                    if (item is CheckBox)
                    {
                        if (((CheckBox)item).Checked == true)
                        {
                            ls.Add(((CheckBox)item).Text.Substring(0, 2));
                        }

                    }
                }

                if (f3 != null)
                {
                    f3(ls);
                }

                this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
复制代码

  可能会有疑问,不就是传一个List<String>吗,有必要这么麻烦吗?其实这里只是利用委托做事情,委托的其他用处还很广泛,我也在学习之中,以后有什么值得记录的东西,再在这里记录。。。

猜你喜欢

转载自www.cnblogs.com/DoudouZhang/p/9804252.html