c# winForm delegate event 跨窗口修改其它控件属性


Form1代码

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();          
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.ToString();
            Form2 form2 = new Form2();
            form2.event_getstr += new getstr(_setstr);
            form2.event_setstr += new setstr(_getstr);
            form2.Show();
        }

        void _getstr(string str)
        {
            textBox1.Text = str;
        }
        string _setstr()
        {
            return this.textBox1.Text;
        }
    }

Form2代码

    public delegate string getstr();
    public delegate void setstr(string str);
    public partial class Form2 : Form
    {
        public event getstr event_getstr;
        public event setstr event_setstr;
        public Form2()
        {
            InitializeComponent();           
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = event_getstr();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            event_setstr(this.textBox1.Text);
        }
    }


你可能感兴趣的:(delegate)