c# winform问题

潶洞 2009-12-01 02:45:32
在winform中有form1和form2
如form1为启动页
怎样从form1中的控件启动form2,并且启动form2的同时将form1关闭(不是隐藏)?
...全文
132 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dong2006 2009-12-01
  • 打赏
  • 举报
回复
在Program类中
static class Program
{
internal static ApplicationContext context = new ApplicationContext(new Form1());
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();

//Application.SetCompatibleTextRenderingDefault(false);
Application.Run(context);
}
}

然后这样显示窗体:
Form2 form = new Form2();
Program.context.MainForm = form;
form.Show();
this.Close();

就可以将上下文的主窗体设置为新的Form2了,然后关闭form1就不影响程序运行了
qqiuzaihui 2009-12-01
  • 打赏
  • 举报
回复
    static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

LeftFrameTest frm = new LeftFrameTest();
frm.ShowDialog();
if (frm.bSign)
Application.Run(new Form1());
else
Application.Exit();
}
}


//======================================================================
//
// Copyright : SHANGHAI HOMEN INTELLIGENT SYSTEM Co.,LTD.
// All rights reserved
//
// Filename : LeftFrameTest
// Description :
//
// created by 邱在辉 at 2009-12-1 14:17:01
//
//======================================================================

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

namespace WindowsApplication1
{
public partial class LeftFrameTest : Form
{
public bool bSign = false;
public LeftFrameTest()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.bSign = true;
this.Close();
}
}
}
jack15850798154 2009-12-01
  • 打赏
  • 举报
回复
你参考一下这个吧也许对你有帮助: http://topic.csdn.net/t/20050614/11/4080787.html
goto_code 2009-12-01
  • 打赏
  • 举报
回复
MDI窗体咯
xiaoping04408 2009-12-01
  • 打赏
  • 举报
回复
你可以在form1里的shown事件中进行启动from2 再把自身关闭
我觉得你怎么说的有点像flash窗体
ProjectDD 2009-12-01
  • 打赏
  • 举报
回复
其实方法还有很多,比如 让F1,F2被包含在另一个类里面

F1打开F2时通知这个类去做,而打开F2函数里首先关闭(dispose())F1

和上面我回复的差不多
lang_csdn 2009-12-01
  • 打赏
  • 举报
回复
不能实现!这个要求真BT!
ProjectDD 2009-12-01
  • 打赏
  • 举报
回复
using System;
using System.Windows.Forms;
using System.Threading;

class Program {
[STAThread]
static void Main() {
Form f1 = new Form(){ Text = "f1" };
f1.Click += delegate {
f1.Close();
Thread t = new Thread(() => {
Form f2 = new Form() { Text = "f2" };
//f2.ShowDialog();
Application.Run(f2);
});
t.Start();
};
f1.ShowDialog();
}
}

//经尝试,效果上说好象还行,
hhc123 2009-12-01
  • 打赏
  • 举报
回复
我个人是不行的啦,看高人的吧
crius 2009-12-01
  • 打赏
  • 举报
回复
关闭就是 .close()
trunjun 2009-12-01
  • 打赏
  • 举报
回复
好像是只有隐藏,
Form2 form2=new Form2();
form1.hide();
form2.show();
liherun 2009-12-01
  • 打赏
  • 举报
回复
如果加载的时候就加载的form1的话,你这样的要求不能实现
Snoworld 2009-12-01
  • 打赏
  • 举报
回复
无解。。。
除非隐藏。。。

111,123

社区成员

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

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

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