vs里面建立一个 winform 应用程序,。 使用工具箱添加TextBox 控件,会自动在InitializeComponent()中添加 private System.Windows.Forms.TextBox textBox1,对比注释行添加的 t2 ,为什么textBox1 在其它方法中是可见的。t2不是(这个好理解,局部对象);
namespace WindowsApplication46
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//TextBox t2=new TextBox();
//this.controls.add(t2);
} private void Form1_Load(object sender, EventArgs e)
{
}
}
}
namespace WindowsApplication46
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//TextBox t2=new TextBox();
//this.controls.add(t2);
} private void Form1_Load(object sender, EventArgs e)
{
}
}
}
1: TextBox1是在 Form1()里面的 InitializeComponent()中 声明的 ,为什么在 Form1_Load()里面能访问到,
2:不修改 t2 声明的位置 如何使得 Form1_Load()中能访问到t2,
#region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(112, 89);
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;
//
// 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); } #endregion private System.Windows.Forms.Button button1;这个是我刚刚建的一个窗体应用程序,button1是在InitializeComponent()方法外面声明的,在InitializeComponent()方法里面初始化,不知道你的代码是不是也是这样。
你在 form1里面
声明全局的
public static aa;再另外的一个form里面一样可以访问
partial class Form1
{
private void InitializeComponent()
{ }
#endregion private System.Windows.Forms.TextBox textBox1;
}
这个的功劳让同一个类 可以写在不同在文件里