不知道一句话两句话是否能说明白. :)public void MakeOnTop()
{
myTopForm.TopMost=true;
}
这里面myTopForm是一个对象实例,从命名规则上都能看处来.用类是不行的.
其实也没有这个必要写这个函数,用到的地方,直接写
this.TopMost = true;就好了.
第二个是一样的问题,缺省的实例是this,这个就表示当前类的当前实例.
MessageBox.Show(this.textBox1.Text );
{
myTopForm.TopMost=true;
}
这里面myTopForm是一个对象实例,从命名规则上都能看处来.用类是不行的.
其实也没有这个必要写这个函数,用到的地方,直接写
this.TopMost = true;就好了.
第二个是一样的问题,缺省的实例是this,这个就表示当前类的当前实例.
MessageBox.Show(this.textBox1.Text );
第而个问题,我指的是从Form2的button1_click()事件中的代码访问Form1,也是出错!
{ this.TopMost=true; }
这样就好了,我用过是窗体置顶
你不妨把全部的代码贴出来,大家看看谁可以修改一下再讨论.
public void MakeOnTop()
{
this.TopMost=true;
}
我定义了两个窗体,想在Form2单击Button1时显示Form1的文本框的内容,但是,执行以下代码却出错,显示“非静态成员......”private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(Form1.textBox1.Text); //就是这里出错了!! }以下是两个窗体的完整代码:/******************************** Form1 ********************************/using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 96);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(40, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(176, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1});
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=new Form2();
Form2.ShowDialog();
}
}
}
//********************************* Form2 *********************************/using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication2
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(176, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(Form1.textBox1.Text); //就是这里出错!!!
}
}
}
天啊!!!!怎么这么麻烦??VB完全不用考虑它是类还是实例,Form1就是Form1,马上可以用,按照C#这样搞法,如果多几个窗体,各自建立这么多引用,岂不烦死了?
各位程序员高手,快出来说句话吧~~~~~~~~!!!!!
private System.Windows.Forms.TextBox textBox1;
改为
public System.Windows.Forms.TextBox textBox1;在Form2中增加
public Form1 myForm1;然后把
Form2 Form2=new Form2();
Form2.ShowDialog();
改为
Form2 myForm2=new Form2();
myForm2.myForm1=this;
myForm2.ShowDialog();
MessageBox.Show(Form1.textBox1.Text);
改为
MessageBox.Show(this.myForm1.textBox1.Text);