积极答复者
两种窗体传值方法的区别和优缺点!

问题
-
第一种:学习视频里的方法,为窗体设置两个public属性,来进行传值,具体见下面的代码:
//窗体Form1代码(主窗体,点击按钮弹出窗体Form2) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinFrom1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); this.label1.Text = "输入姓名……"; if (frm.ShowDialog() == DialogResult.OK) { this.label1.Text = string.Format("{0},{1}", frm.Username, frm.Sex); } else this.label1.Text = "请输入……"; } } } //窗体Form2代码(AcceptButton设置为button1,CancelButton设置为button2) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinFrom1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public String Username { get { return this.textBox1.Text; } set { this.textBox1.Text = value; } } public String Sex { get { if (this.radioButton1.Checked) { return "男"; } return "女"; } set { if (value == "男") { this.radioButton1.Checked = true; } else { this.radioButton2.Checked = true; } } } } }
第二个方法是我自己琢磨的,具体方法如下://窗体Form1_1为主窗体,点击按钮弹出窗体Form2_2,主窗体上有用来显示返回值的label1(label1的modifiers设置为Public) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinFrom1 { public partial class Form1_1 : Form { public static Form1_1 frm1_1; public Form1_1() { frm1_1 = this; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2_2 frm = new Form2_2(); frm.ShowDialog(); } } } //窗体Form2_2代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinFrom1 { public partial class Form2_2 : Form { public Form2_2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (this.radioButton1.Checked) { Form1_1.frm1_1.label1.Text = String.Format("{0},{1}", this.textBox1.Text, this.radioButton1.Text); } else { Form1_1.frm1_1.label1.Text = String.Format("{0},{1}", this.textBox1.Text, this.radioButton2.Text); } Form2_2.ActiveForm.Dispose(); } } }
- 已移动 Sheng Jiang 蒋晟Moderator 2011年7月12日 19:00 (发件人:Visual C#)
答案
-
你好:)
其实第二种方法可以近似理解成第一种方法的变通。你公开了Form1的某个部分(或者是公开了整个Form1)。那么当然可以在Form2中直接访问Form1的属性什么的了。
但是说老实话,我不建议这样做——这显然违反了OOP的原则:不应当将私有变量公开化。
视频中的例子好处在于——封装了私有变量,通过公共属性进行赋值。
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 雨木木 2011年7月15日 10:07
全部回复
-
你好:)
其实第二种方法可以近似理解成第一种方法的变通。你公开了Form1的某个部分(或者是公开了整个Form1)。那么当然可以在Form2中直接访问Form1的属性什么的了。
但是说老实话,我不建议这样做——这显然违反了OOP的原则:不应当将私有变量公开化。
视频中的例子好处在于——封装了私有变量,通过公共属性进行赋值。
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 雨木木 2011年7月15日 10:07