自己总结的窗体传值3---委托:

窗体1的textbox1的值显示在窗体2的textbox1上面和窗体3上面并且做适当的处理
自己总结的窗体传值3---委托:_第1张图片—使用委托;
在主窗体一定一个委托,
其他窗体想要使用,就给这个委托赋值就可,也就是想办法将窗体2和窗体3的方法传到窗体1里面就可以。
踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩

	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 WindowsFormsApp6
{
    public delegate void Mydele(string ss);//定义了一个委托类型
    public partial class Form1 : Form
    {
    public Mydele mydele;//定义一个委托变量
   
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
		//此时将窗体chuildform1进入到form里面
        ChildForm1 childForm1 = new ChildForm1();
      
      //给委托变量赋值
        if (mydele==null )
        {
            mydele = new Mydele(childForm1.SetText)
        } 
        else
        {
            mydele+= new Mydele(childForm1.SetText);
        }
        ChildForm2 childForm2 = new ChildForm2();
        if (mydele==null )
        {
            mydele = new Mydele(childForm2.GetSetText);
        }
        else
        {
            mydele += new Mydele(childForm2.GetSetText);
        }
        childForm1.Show();
        childForm2.Show();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        mydele(this.textBox1.Text);//输入委托变量的参数
    }
}

}

草草草草草草草草草草草草草草草草

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 WindowsFormsApp6
{
    public partial class ChildForm2 : Form
{
    public ChildForm2()
    {
        InitializeComponent();
    }

    public void GetSetText(string ss)
    {
        this.textBox1.Text = ss+"---You";
    }

    private void ChildForm2_Load(object sender, EventArgs e)
    {

    }
}

}

踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩

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 WindowsFormsApp6
{
    public partial class ChildForm1 : Form
    {
   
    public ChildForm1()
    {
        InitializeComponent();
    }

    public void SetText(string ss)
    {
        this.textBox1.Text = ss;
    }

    private void ChildForm_Load(object sender, EventArgs e)
    {

    }
}

}

自己总结的窗体传值3---委托:_第2张图片

委托就是 自己准备好参数, 让别人带着他的方法过来让我使用

你可能感兴趣的:(winfrom)