打开一个新窗体,关闭旧窗体 C#

发布于 2024-12-20 10:56:17 字数 734 浏览 0 评论 0原文

我对 C# 有点陌生,我正在自学,尝试制作一个具有多种功能的程序来教自己如何使用 C#。如果我不知道什么,我通常会上网查看,但这让我发疯。

我记得一开始我开始这个,我想打开一个表单并关闭旧的表单,但是当我关闭新的表单时,旧的表单会再次出现,以及这个问题的其他奇怪的变体。 this.Hide() 似乎也没有做任何事情。

目前,为了打开一个新表单,我正在使用这段代码,但感觉应该有 1 行代码来完成像打开表单这样简单的事情...... 我的问题是是否有。

    private void OpenMainForm()
    {
        MainForm frm2 = new MainForm();
        frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
        frm2.Show();

        // Since this.Hide() for some reason doesn't work, i'll have to do this crap
        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
    }

    private void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }

I'm kinda new to C# and I'm doing self study by trying to make a program with a variety of functions to teach myself how to work with C#. I usually look at the internet if I don't know something but this has been driving me crazy.

I remember in the very beginning i started this that I wanted to open a form and close the old one, but when i closed the new form, the old form would reappear again, and other weird varieties of this issue. this.Hide() didn't seem to do anything either.

Currently for opening a new form I'm using this code, but it feels like there should be something with 1 line of code for something as simple as opening a form...
My question is if there is.

    private void OpenMainForm()
    {
        MainForm frm2 = new MainForm();
        frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
        frm2.Show();

        // Since this.Hide() for some reason doesn't work, i'll have to do this crap
        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
    }

    private void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不及他 2024-12-27 10:56:17

如果您想在辅助窗口中隐藏主窗口,则应使用 ShowDialog() 方法。这样,您甚至不需要 form_close 事件

您的代码应如下所示:

private void OpenMainForm()
{
    MainForm frm2 = new MainForm();
    this.Hide();           //Hide the main form before showing the secondary
    frm2.ShowDialog();     //Show secondary form, code execution stop until frm2 is closed
    this.Show();           //When frm2 is closed, continue with the code (show main form)
}

If you want to hide your main window when you're in the secondary one, you should use the ShowDialog() method. With that, you won't even need the form_closed event.

Your code should look like:

private void OpenMainForm()
{
    MainForm frm2 = new MainForm();
    this.Hide();           //Hide the main form before showing the secondary
    frm2.ShowDialog();     //Show secondary form, code execution stop until frm2 is closed
    this.Show();           //When frm2 is closed, continue with the code (show main form)
}
三岁铭 2024-12-27 10:56:17

您还可以使用此代码:

public static void ThreadProc()
{
    Form2 f; 
    Application.Run(new Form2());
}

private void button1_Click(object sender, EventArgs e)
{
    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
    t.Start();
    this.Close();
 }

You can also use this code:

public static void ThreadProc()
{
    Form2 f; 
    Application.Run(new Form2());
}

private void button1_Click(object sender, EventArgs e)
{
    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
    t.Start();
    this.Close();
 }
妞丶爷亲个 2024-12-27 10:56:17

这对我来说非常适合,

Form2 frm = new Form2();
frm.Show();
frm.Activate();
this.Hide();

但如果你想从 Form2 关闭整个应用程序...你必须在 Form2 的 FormClosing 事件中添加 Application.Exit();

This works perfectly for me

Form2 frm = new Form2();
frm.Show();
frm.Activate();
this.Hide();

but if you want to close the whole application from Form2...you have to add Application.Exit(); in FormClosing event of Form2

巡山小妖精 2024-12-27 10:56:17

您可以隐藏旧表单,如下所示。

private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Hide();
}

You can hide old form as below.

private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Hide();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文