关于窗体隐藏后,再显示的问题。

_bobo 2003-06-28 10:32:22


在主窗体(FORM1.CS)中当按了某个BUTTON后,把主窗体给隐藏起来(this.hide()),同时把

LOAD 和 SHOW 另外一个窗体(FORM2.CS). 现在如果关闭FORM2,那么如何显示FORM1


也就是说如何FORM2.cs 中调用FORM1???

另外有一点补充的是。 FOMR1 和FORM2 都是在主程序中动态创建的,先是在主程序中创建FORM1,如下的代码:
[STAThread]
static void Main()
{
Application.Run(new form1());
}


然后在FORM1 运行起来后,在FORM1 上的某个BUTTON 上添加代码,如果在程序中运行到,BUTTON 中的代码,把FORM1 隐藏,创建FORM2的实例。如下代码:

this.Hide();
partsForm = new Form2();
partsForm.Show();
partsForm.Activate();
...全文
248 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SimonSnow 2003-06-29
  • 打赏
  • 举报
回复
赞同 TheAres(班门斧)
TheAres 2003-06-28
  • 打赏
  • 举报
回复
还是看下面的代码吧,

Form1.cs,Form2.cs,拷贝到你的程序中,看看会不会出错。注意是两个文件。

using System;
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();
}
}
}
_bobo 2003-06-28
  • 打赏
  • 举报
回复
你说的方法我已经测试过了。
在FORM1 中 并不能使用
partsForm.myForm1 = this; 这条语句,也就是说在FORM2 定义的变量,并不能在FORM1带码中被调用。只有在FORM2 的代码中,才可以调用。
TheAres 2003-06-28
  • 打赏
  • 举报
回复
APPLICATION 并不维护每一个你打开窗体的实例。this是本类的一个实例。

跟这个问题没有关系吧?

如果不怕麻烦,你可以用FindWindows把这个窗体找出来,然后发送消息让他显示。
_bobo 2003-06-28
  • 打赏
  • 举报
回复
这样做肯定是可以的。
能不能从系统角度来解决这个问题,也就是使用APPLICATION 类,或THIS 指针 。
TheAres 2003-06-28
  • 打赏
  • 举报
回复
在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();

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧