===============================================
没事做。突然想写点给C#菜鸟的东东。其实我也很菜。>_<大家相互交流麻。希望高手们不要见笑。有兴趣的朋友可以来交流一下。
===============================================
QQ:1325625
MSN: CnBabyCrazy@HotMail.Com
===============================================
进入正题:
其实窗体传值有很多方法。比如说申明一个Static变量.但是这样的方法过于死版。不够灵活。下面介绍一个我认为最灵活的方法。以下代码编译无错。
(代码如下):
/
===============================================
//注释 Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DotNet_WinForm窗体传值_方法一_
{
/// <summary>
/// BabyCrazy
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
public System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 0;
this.label1.Text = "待修改的:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(112, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "菲菲";
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Show窗体2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(32, 128);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 128);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "textBox1的Modifiers属性修改为/"Public” ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(144, 23);
this.label2.TabIndex = 4;
this.label2.Text = "C#WinForm传值方法(一)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//使用带参构造函数生成Form2实例
Form2 frm2 = new Form2(this);
frm2.Show();
}
}
}
===============================================
//注释Form2.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace DotNet_WinForm窗体传值_方法一_
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private object transferFrm;//用于接受传值
/// <summary>
/// BabyCrazy
/// </summary>
private System.ComponentModel.Container components = null;
public Form2(object frm)
{
InitializeComponent();
//接受传值
transferFrm = frm;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 40);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "菲菲我爱你!";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 1;
this.label1.Text = "修改的值:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "修改窗体1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(24, 128);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 128);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "此程序献给和我一样菜的朋友们和我爱的人。欢迎大家与我交流。Qq:1325625";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
//把object强制转换成Form1类型。理论上是可以修改任何窗体。>_<
((Form1)transferFrm).textBox1.Text = this.textBox1.Text;
}
}
}
===============================================
完!~
没事做。突然想写点给C#菜鸟的东东。其实我也很菜。>_<大家相互交流麻。希望高手们不要见笑。有兴趣的朋友可以来交流一下。
===============================================
QQ:1325625
MSN: CnBabyCrazy@HotMail.Com
===============================================
进入正题:
其实窗体传值有很多方法。比如说申明一个Static变量.但是这样的方法过于死版。不够灵活。下面介绍一个我认为最灵活的方法。以下代码编译无错。
(代码如下):
/
===============================================
//注释 Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DotNet_WinForm窗体传值_方法一_
{
/// <summary>
/// BabyCrazy
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
public System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 0;
this.label1.Text = "待修改的:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(112, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "菲菲";
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Show窗体2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(32, 128);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 128);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "textBox1的Modifiers属性修改为/"Public” ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(144, 23);
this.label2.TabIndex = 4;
this.label2.Text = "C#WinForm传值方法(一)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//使用带参构造函数生成Form2实例
Form2 frm2 = new Form2(this);
frm2.Show();
}
}
}
===============================================
//注释Form2.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace DotNet_WinForm窗体传值_方法一_
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private object transferFrm;//用于接受传值
/// <summary>
/// BabyCrazy
/// </summary>
private System.ComponentModel.Container components = null;
public Form2(object frm)
{
InitializeComponent();
//接受传值
transferFrm = frm;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 40);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "菲菲我爱你!";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 1;
this.label1.Text = "修改的值:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "修改窗体1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(24, 128);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 128);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "此程序献给和我一样菜的朋友们和我爱的人。欢迎大家与我交流。Qq:1325625";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
//把object强制转换成Form1类型。理论上是可以修改任何窗体。>_<
((Form1)transferFrm).textBox1.Text = this.textBox1.Text;
}
}
}
===============================================
完!~