当前位置:文档之家› C#_两个WIN程序窗口间传递参数的方法

C#_两个WIN程序窗口间传递参数的方法

法二: 进入 VS2005 后,大家可以发现子窗体操作父窗体不能沿用 2003 下的方法:把父 窗体的空间访问属性由 private 改为 public.IDE 已经把控 件声明这部分代码隐 藏了,所以只有采用更加对象一点的方法。 父窗体与子窗体间的参数传递我采用的步骤如下: 1 父窗体中声明一个静态的父窗体类型的临时对象 public static frmFather frmFatherTemp; 2 父窗体构造函数中对该变量赋值 public frmFather() { InitializeComponent(); frmFatherTemp = this; } 3 把要传递的参数设置为父窗体的一个属性,并设置访问器。访问其的 set 方法 中进行了参数与父窗体控件绑定的操作。 private string testValue; public string TestValue { get { return testValue; } set { this.testValue = value;
方法一: C#中没有了像 中的全局变量,那么我们如何实现在不同的页面间传递参 数呢? 下面举例说明如何实现这一功能. 1.新建一个项目. 2.在该工程中添加一个窗体 Form1. 3.在该窗体中定义静态型字符串变量 myTestStr1: public static string myTestStr1=""; 4. 在 该 窗 体 的 构 造 函 数 中 对 该 变 量 进 行 赋 值 , 并 为 该 窗 体 类 添 加 属 性 GetStrValue. public Form_Form1() { InitializeComponent(); myTestStr1="Hello!"; } public string GetStrValue { get { return myTestStr1; } set { myTestStr1=value; } } 5.在该工程中另添加一个窗体 Form2. 6.在 Form1 窗体上添加一个 button 按钮(name:but_Test); 7.在 Form1 窗体的 but_Test_Click 事件中添加以下代码: private void but_Test_Click(object sender, System.EventArgs e) { Form2 frm1=new Form2(); frm1.ShowDialog(this) ; frm1.Close(); } 8.在 Form2 窗体上添加一个 button 按钮(name:but_Yes); 9.在 Form1 窗体的 but_Yes_Click 事件中添加以下代码: private void but_Yes_Click(object sender, System.EventArgs e) { MessageBox.Show (Form_Form1.myTestStr1 ); //直接访问. 显示.结 果:" Hello!"
using using using using using using using
System; System.Collections.Generic; ponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms;
namespace winFormParameterPass {
public partial class frmFather : Form { public static frmFather frmFatherTemp; private string testValue; public string TestValue { get { return testValue; } set { this.testValue = value; this.txtFather.Text = value; } } public frmFather() { InitializeComponent(); frmFatherTemp = this; } private void btnFather_Click(object sender, EventArgs e) { this.TestValue = this.txtFather.Text; frmSon frm = new frmSon(); frm.ShowDialog(); } } } frmSon.cs using System; using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace winFormParameterPass {
中调用父窗体,能够间接用“this.pParentWin”就能够了 不过以上所做的,只是让你能够访问当前主窗体对象,那么如何操做控件,很多 人间接修改控件的成员访问符, 即把“private”改为 “public”,我觉得这样 破坏了本身类的封装,所以我比较喜欢的做法是增加公有属性或方法来供调用, 例如: public string ButtonText { get{ return btn.Text;} set{ btn.Text = value;} } public void Button_Click() { this.btnDConvert.PerformClick();//Execute button click } 转 载 自 : /bell_blue/blog/item/9474a7cc2031e41a00e928f8.html
this.txtFather.Text = value; } } 4 父窗体参数传递事件中对要传递的参数赋值,并打开子窗体。父窗体的工作到 此结束。 this.TestValue = this.txtFather.Text; frmSon frm = new frmSon(); frm.ShowDialog(); 5 子窗体构造函数中设置传递参数与子窗体控件的绑定操作 public frmSon() { InitializeComponent(); this.txtSon.Text = frmFather.frmFatherTemp.TestValue; } 6 子窗体回传事件中,对父窗体的临时对象的该参数属性赋值 private void btnSon_Click(object sender, EventArgs e) { frmFather.frmFatherTemp.TestValue = this.txtSon.Text; this.Close(); } ok。一切搞定! 全部代码如下: frmFather.cs
Form_Form1 frm2=new Form_Form1(); frm2.GetStrValue ="How do you do?"; //生成一个新的实例对该静态变量进行操作(修改该静态变量的 值). MessageBox.Show (frm2.GetStrValue ); //通过该 实例的内部成员对它进行访问 .显示.结果: How do you do?" MessageBox.Show (Form_Form1.myTestStr1 ); How do you do?" //直接访问. 显示.结果:"
public partial class frmSon : Form { public frmSon() { InitializeComponent(); this.txtSon.Text = frmFather.frmFatherTemp.TestValue; } private void btnSon_Click(object sender, EventArgs e) { frmFather.frmFatherTemp.TestValue = this.txtSon.Text; this.Close(); } } } ---------分割线----------public static int myPI = 3.14; 这样就可以在工程中的任何地方引用这个全局变量了 应用方法,类名.myPI 另外方法: Form2 form2=new Form2(this); 很多人都苦恼于如何在子窗体中操做主窗体上的控件, 或者在主窗体中操做子窗 体上的控件。相比较而言, 后面稍微简单一些,只需在主窗体中创建子窗体的 时候,保留所创建子窗体对象即可。 下面重点引见前一种,目前常见的有两种方法,基本上大同小异: 第一种,在主窗体类中定义一个静态成员,来保存当前主窗体对象,例如: public static yourMainWindow pCurrentWin = null; 然后在主窗体构造函数中,给静态成员初始化,如下: pCurrentWin = this; 那么在子窗体中调用父窗体,能够通过“主窗体类名. pCurrentWin”来操做当 前的主窗体。 第二种,是在子窗体中定义一个私有成员,来保存当前主窗体对象,例如: private yourMainWindow pParentWin = null; 然后在子窗体构造函数中,加一参数,如下: public yourChildWindow( yourMainWindow WinMain ) { pParentWin = WinMain; //Other code } 在主窗体创建子窗体的时候,要把 this 做为参数来构造子窗体,这样在子窗体
相关主题