窗体之间传值的方法

第一种方法:
创建一个类,里面声明用于存储接收的字段。传的时候存储于字段中,要用的时候,直接类名.字段名 进行调用。(这种方法传递是双向的)

第二种方法:
1.在Form1里定义
public string Name = "*****"


2. 在Form2里创建Form1对象,
Form1 f = new Form1();
然后就可以通过f.Name取值了

(不过这样不安全)

第三种方法:用构造函数


在窗体Form2中
int value1;
string value2;


public Form2 ( int value1 , string value2 )
{
     InitializeComponent ( );

     this.value1 = value1;
     this.value2 = value2;
}


在窗体Form1中这样调用

new Form2 ( 111 , "222" ).Show ( );

这样就把111,"222",这2个值传送给了Form2  d
但是这样的传值是单向的

第四种方法:通过窗体的公有属性值(特点:实现简单)
举例“在窗体Form2中定义一个公有属性Form2Value,获取和设置textBox1的文本值

public string Form2Value
{
     get
     {
          return this.textBox1.Text;
     }
     set
     {
         this.textBox1.Text = value;
     }
}


在窗体Form1中这样调用

Form2 f2 = new Form2 ( );
f2.Form2Value = "Ok"; //给Form2的textBox1赋值Ok
f2.ShowDialog ( );

 
第五种方法:通过窗体的公有属性值和Owner属性(特点:实现简单,灵活)

在窗体Form1中

public int Form1Value = 1;

Form2 f2 = new Form2 ( );
f2.ShowDialog ( this ); //把Form1作为Form2的所有者传递给Form2

在窗体Form2中


//Form2的所有者是Form1
Form1 f1 = ( Form1 ) this.Owner;
//取到Form1的值
MessageBox.Show ( f1.Form1Value .ToString ( ) );
//给Form1的Form1Value赋值222
f1.Form1Value = 222;


 
第六种方法:通过窗体的公有属性值和Application.OpenForms属性(感觉用的比较少)
说明:Application.OpenForms属性:获取属于应用程序的打开窗体的集合。(此属性在 .NET Framework2.0版中)
实现代码如下:
在窗体Form1中

public int Form1Value = 1;

Form2 f2 = new Form2 ( );
f2.Show ( );

在窗体Form2中


string formName = "Form1";
Form fr = Application.OpenForms [ formName ];

if ( fr != null )
{
     Form1 f1 = ( Form1 ) fr;
     //取到Form1的值是1
     MessageBox.Show ( f1.Form1Value.ToString ( ) );
     //给Form1的Form1Value赋值222
     f1.Form1Value = 222;
}


 
第七种方法:通过委托

Form2中的代码

 1  public partial class Form2 : Form
 2     {
 3         public Form2()
 4         {
 5             InitializeComponent();
 6         }
 7        
 8         public Form2(string n, Action<string> updateText): this()
 9         {
10             this.textBox1.Text = n;
11             this._update = updateText;
12         }
13        //这里定义一个委托来接受Form1传过来的方法
14         private Action<string>  _update;
15 
16         private void button1_Click(object sender, EventArgs e)
17         {
18             //1.将当前窗体中的文本框中的值,赋值给“窗体1”中的文本框
19             //2.这里调用Form1传过来的方法,来操作
20             this._update(textBox1.Text.Trim());
21 
22             //关闭窗体2
23             this.Close();
24         }
25     }
View Code

Form1中的代码

 1  public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             //把Form1中的方法UpdateTextBox传到Form2中,来操作Form1中的TextBox
11             Form2 f2 = new Form2(textBox1.Text.Trim(), UpdateTextBox);
12             f2.Show();
13         }
14 
15         //Form1中方法,来改变txt的值
16         private void UpdateTextBox(string val)
17         {
18             this.textBox1.Text = val;
19         }
20 
21     }
View Code

第八种方法:通过事件
在窗体Form2中定义公有属性Form2Value,获取和设置textBox1的文本值
并且还定义一个accept事件


public string Form2Value
{
     get
     {
          return this.textBox1.Text;
     }
     set
    {
         this.textBox1.Text = value;
    }
}

public event EventHandler accept;

private void button1_Click ( object sender , EventArgs e )
{
     if ( accept != null )
     {
          accept ( this , EventArgs.Empty ); //当窗体触发事件,传递自身引用
     }
}


在窗体Form1中


Form2 f2 = new Form2 ( );
f2.accept += new EventHandler ( f2_accept );
f2.Show ( );

void f2_accept ( object sender , EventArgs e )
{
     //事件的接收者通过一个简单的类型转换得到Form2的引用
     Form2 f2 = (Form2) sender;
     //接收到Form2的textBox1.Text
     this.textBox1.Text = f2.Form2Value;
}

原文地址:https://www.cnblogs.com/nanxiaoxiang/p/5171491.html