c#winForm delegate eventウィンドウ間で他のコントロール属性を変更

1504 ワード



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);
        }
    }