两个窗体,如何做到对其中一个的操作影到到另一个??
如下:class form1{}和class form2{},其中form1中有一个button控件与一个textbox控件,我想要做到点击这个BUTTON时,form2中的某个变量能自动赋值为textbox的值
程序代码:
using System; using System.Collections.Generic; using using System.Windows.Forms; namespace FormTest { public partial class Form1 : Form { private bool hasOpenForm = false; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (!this.hasOpenForm) { this.hasOpenForm = true; Form2 form = new Form2(); form.StartPosition = FormStartPosition.CenterScreen; form.OnTransferText += new EventHandler<Form2.TransferTestEventArgs>(form_OnTransferText); form.FormClosed += new FormClosedEventHandler(form_FormClosed); form.Show(); } } void form_FormClosed(object sender, FormClosedEventArgs e) { this.hasOpenForm = false; } void form_OnTransferText(object sender, Form2.TransferTestEventArgs e) { this.textBox1.Text = e._Text; } } }
Form2:
程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FormTest { public partial class Form2 : Form { public event EventHandler<TransferTestEventArgs> OnTransferText; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { if (sender != null) { OnTransferText(this, new TransferTestEventArgs(this.textBox1.Text)); } } public class TransferTestEventArgs : EventArgs { public readonly string _Text; public TransferTestEventArgs(string Text) { _Text = Text; } } } }