Form1是主窗体,就是打开程序后显示后的第一个窗体。它关闭后,其它窗体都会关闭,而且整个程序就退出了。Form1里有一个TextBox1和一个按钮Button1。
private void Button1_Click(object sender,EventArgs e){Form2 f2=new Form2();f2.Show();}
打开这个Form2里有一个TextBox2和一个按钮Button2。那么,如何把这个TextBox2的内容,传递给主窗体Form1的TextBox1?private void Button2_Click(object sender,EventArgs e){把Form2的TextBox2的内容,传递回主窗体Form1的TextBox1里}请问,C#具体如何实现?
private void Button1_Click(object sender,EventArgs e){Form2 f2=new Form2();f2.Show();}
打开这个Form2里有一个TextBox2和一个按钮Button2。那么,如何把这个TextBox2的内容,传递给主窗体Form1的TextBox1?private void Button2_Click(object sender,EventArgs e){把Form2的TextBox2的内容,传递回主窗体Form1的TextBox1里}请问,C#具体如何实现?
frm.closing+=new eventhandle(方法名);
frm.show();
方法名(object sender,event e)
{
form2 frm=sender as form2;
this.textBox1.text=frm.屬性;
}
public delegate void FindResult(String text); public event FindResult ResultFinded; private void button1_Click(object sender, EventArgs e)
{
string qustring = this.textBox1.Text;
SendFindInfo(qustring); }
private void SendFindInfo(string query,ref int pos)
{
if (ResultFinded != null)
{
ResultFinded(query);
}
}
主窗体 private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ResultFinded += new Form2.FindResult(frm_ResultFinded);
frm.Show();
} void frm_ResultFinded(string text)
{
this.textBox1.text= text;
}
public delete void SetMethod(string text);
在Form2構造函數中寫個有參數的,接收委拖
setMethod _d;
public form2(SetMethod d)
{
_d=d;
}Button2 click事件中
{
_d(textBox2.text); 這樣就行了
}form1中 button事件
{
form2 frm=new form2(new setMethod(set));
frm.show();
}public void Set(string text)
{
textBox1.text=text;
}
主窗口public string TB1
{
get { return textbox1.text; }
set { textbox1.text = value; }
} Form2 nextForm1 = new Form2();
nextForm1.Owner = this;
nextForm1.ShowDialog();
子窗口Form1 fatherForm = (Form1)this.Owner;
Form1.TB1=this.textbox2.text;
{
Form2 frm2 = new Form2(this);
frm2.Owner = this;
frm2.Show();
}第二个窗体: public Form2(Form1 frmM)
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form1 frmM = (Form1)this.Owner;
frmM.textBox1.Text = this.textBox2.Text;
}把主窗体的textbox1的可见属性设为public就OK了