我这儿做了一个例子,看是否是你所说的,不访看一看!
例子说明:在项目中新两个窗体:一个是Form1并且在Form1上有一Button按钮,另一个是Form2,达到标是将Form1实例对象传给Form2,同时Form2中还能访问Form1中的Button按钮。
代码如下:
----------Form1窗体的代码
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(99, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
/// <summary>
/// 将按钮控件设为public
/// </summary>
public System.Windows.Forms.Button button1;
/// <summary>
/// 创建Form2窗体对象
/// </summary>
private Form2 _frmForm2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
_frmForm2.FrmTest = this;
_frmForm2.Show();
}
}
--------Form2窗体的代码:
public class Form2 : Form
{
private Form1 _frmTest;
/// <summary>
/// 获取或设置窗体Form1对象
/// </summary>
public Form1 FrmTest
{
get { return _frmTest; }
set { _frmTest = value; }
}
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.FrmTest.button1.Text = "修改Form1窗体按钮Text属性";
}
}
[此贴子已经被作者于2008-4-9 8:49:23编辑过]