在FormA中打开FormB,同时将自身(FormA)关闭,请问,如何实现?

夜雨山庄 2007-07-12 02:22:37
FormA中有一个BUTTON,点击后自身(FormA)关闭,
显示FormB,请问,如何实现,谢谢.
...全文
206 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigthomas 2007-07-16
  • 打赏
  • 举报
回复
//*************************************************************************
// class FormMain
//*************************************************************************

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Bigthomas.twoForms
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FormMain : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public FormMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
FormA fa=new FormA();
fa.Show();
}

/// <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()
{
//
// FormMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "FormMain";
this.Text = "FormMain";

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FormMain());
}
}
}
//*************************************************************************
// class FormA
//*************************************************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Bigthomas.twoForms
{
/// <summary>
/// FormA 的摘要说明。
/// </summary>
public class FormA : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnShowB;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public FormA()
{
//
// 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.btnShowB = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnShowB
//
this.btnShowB.Location = new System.Drawing.Point(48, 72);
this.btnShowB.Name = "btnShowB";
this.btnShowB.Size = new System.Drawing.Size(192, 32);
this.btnShowB.TabIndex = 0;
this.btnShowB.Text = "ShowB&&CloseA";
this.btnShowB.Click += new System.EventHandler(this.btnShowB_Click);
//
// FormA
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.btnShowB);
this.Name = "FormA";
this.Text = "FormA";
this.ResumeLayout(false);

}
#endregion

private void btnShowB_Click(object sender, System.EventArgs e)
{
FormB fb=new FormB();
fb.Show();
this.Close();
}
}
}
//*************************************************************************
// class FormB
//*************************************************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Bigthomas.twoForms
{
/// <summary>
/// FormB 的摘要说明。
/// </summary>
public class FormB : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public FormB()
{
//
// 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.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "FormB";
}
#endregion
}
}
hahahjx1 2007-07-16
  • 打赏
  • 举报
回复
你初始化的时候,启动的就是A的界面`
`我说的这个是做到Button的CLILK事件里的
yangyunfei 2007-07-16
  • 打赏
  • 举报
回复
hahahjx1() 错的,你一直这样做? 我想你是一直是个糊涂鬼吧
hahahjx1 2007-07-13
  • 打赏
  • 举报
回复
//先实例化FORM
FormB fb = new FormB();
FormA fa = new FormA();
fb.Show();
fa.Close();
这样也行``

其实我一直是这样做的``



shanminmin 2007-07-13
  • 打赏
  • 举报
回复
hahahjx1() 好像不对吧?没有测试,只是感觉不正确。
向你这么说,应该是b显示,a不显示啊。既然a不显示,怎么点a上的按钮?
zh-wall-e 2007-07-12
  • 打赏
  • 举报
回复
不能。。
因为FormA关闭了。。。
整个应用程序的主句柄就关了,这会导致整个应用程序的退出。。
夜雨山庄 2007-07-12
  • 打赏
  • 举报
回复
没有更好的方式吗?
MicroSoftor 2007-07-12
  • 打赏
  • 举报
回复
//先实例
FormB fb = new FormB();
//显式FormB
fb.Show()

//然后隐藏FormA
FormA.Visible = false

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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