c# 窗体传值

2.窗体传值

form1中有textBox1和 button1。

form2中有lable1

要实现的:点击button1后form2显示且lable1的内容是textBox1的内容。

                 实现的代码:

                form1中button的代码:

                                                 String t=textBox1.Text; //把要显示的的内容给一个变量;

                                                 from2 fr =new form2(t);//form2(t)是在form2中的一个重载构造函数

                                                 fr.show();

              form2中的代码:

                                                private string m;

                                                 public form2(string p )//重载构造函数,加一个参数(用来传值)--在form1中实例化的时候,那个参数——t 就是textBox1.text的内容,在此代替的是p

                                                {

                                                    InitializeComponent();

                                                    this.m=p; //把参数赋值给form2的一个私有变量

                                                 }

                                              form2_load( )

                                                {

                                                   this.lable1.text=m; //lable1的内容就是私有变量m,此时的m是由form1传过来的值t                             

                                                  }

                                                           

你可能感兴趣的:(C#)