兩個Form之間用構造函數回傳數值【兩個Form窗體不關閉情況下】

Form1代碼如下:

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 Form1 : Form
    {
        //
        public string text = null;
        //
        public Form1()
        {
            InitializeComponent();
        }
        void form2_MyEvent(string text)
        {
            textBox1.Text = text;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            text = textBox1.Text;
            Form2 form2 = new Form2(text);
            form2.MyEvent += new MyDelegate(form2_MyEvent);
            form2.Show();
        }
    }
}





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;


namespace 窗體間傳遞數值
{
    public delegate void MyDelegate(string text);


    public partial class Form2 : Form
    {


        public event MyDelegate MyEvent;
       
        public Form2(string text)
        {
            InitializeComponent();
            this.textBox1.Text = text;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MyEvent(this.textBox1.Text);


        }
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章