最好的方法是定义返回TextBox对象的属性,在Form1里调用其属性,例如:
Form2:public TextBox TextBox1
{
get { return textBox1; }
set { textBox1 = value; }
}Form1调用Form2的TextBox1属性:
Form2 f = new Form2( );
f.MdiParent = this;
this.textBox1.Text = f.TextBox1.Text;
Form2:public TextBox TextBox1
{
get { return textBox1; }
set { textBox1 = value; }
}Form1调用Form2的TextBox1属性:
Form2 f = new Form2( );
f.MdiParent = this;
this.textBox1.Text = f.TextBox1.Text;
string textB;
B bDialog = new B();if(bDialog.ShowDialog == ResultDialog.OK)
{
textB = bDialog.textBox1.text.toString();
}记得把B中的textBox1的Modifiers改为public,要不然在A中是无法访问的
static void Main()
{
Login login=new Login();
login.ShowDialog();
string uid;
if(login.DialogResult==DialogResult.OK)
{
uid=login.textBox1.Text;
Application.Run(new Form1());
login.Close();
}
}报错:F:\c#\ProductsStatistic\Form1.cs(471): 名称“uid”在类或命名空间“ProductsStatistic.Form1”中不存在如果写在其它地方:
private System.ComponentModel.IContainer components;
private string uid;报错:F:\c#\ProductsStatistic\Form1.cs(385): 非静态的字段、方法或属性“ProductsStatistic.Form1.uid”要求对象引用
{
Login login=new Login();
Form1 form1=new Form1();
login.ShowDialog();
if(login.DialogResult==DialogResult.OK)
{
form1.statusBar1.Text=login.textBox1.Text;
Application.Run(new Form1());
login.Close();
}
}
----------------------------------------------------------不明白,什么“观察着模式”