在Form2类中定义一个变量来保存Form1的一个实例。public Form1 myForm1 = null;然后把Form1打开的Form2的代码修改如下:
this.Hide();
partsForm = new Form2();
partsForm.myForm1 = this;
partsForm.Show();
partsForm.Activate();在Form2的close事件中,
调用
if ( this.myForm1 != null)
this.myForm1.Show();
this.Hide();
partsForm = new Form2();
partsForm.myForm1 = this;
partsForm.Show();
partsForm.Activate();在Form2的close事件中,
调用
if ( this.myForm1 != null)
this.myForm1.Show();
解决方案 »
- 水晶报表 ERROR: 索引超出范围。必须为非负值并小于集合大小。参数名: index
- 如何让parameter像datareader一样可以读取多条记录
- 知道换肤的帮帮忙哦
- .net基础知识
- 寻求查找Method中的一些特定内容的方法
- 有谁用过sqlWhereBuilder服务器控件的field dropdown绑定过数据库字段吗?如何实现的....只有这么多分了.
- 实现复制图片的地址问题?? ??
- 控件连带数据内容问题?
- C#中 listBox控件 选定listbox中的某一项后,就将该选定的项改变颜色,而且该项不可在选取,该如何实现啊?
- 远程调用dcom,出现80070005错误
- 如何在C#中调用Dos命令或.bat命令??
- 关于Window form datagrid 控件响应KeyPress事件问题?急
能不能从系统角度来解决这个问题,也就是使用APPLICATION 类,或THIS 指针 。
在FORM1 中 并不能使用
partsForm.myForm1 = this; 这条语句,也就是说在FORM2 定义的变量,并不能在FORM1带码中被调用。只有在FORM2 的代码中,才可以调用。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 72);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion public static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.Hide();
Form2 partsForm = new Form2();
partsForm.myForm1 = this;
partsForm.Show();
partsForm.Activate(); }
}
}//Form2.cs
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
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1 myForm1 = 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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form2";
this.Text = "Form2";
this.Closed += new System.EventHandler(this.Form2_Closed); }
#endregion private void Form2_Closed(object sender, System.EventArgs e)
{
if (this.myForm1 != null)
this.myForm1.Show();
}
}
}