如何在C#WinForm之间切换? [英] How to switch between C# WinForm?

查看:113
本文介绍了如何在C#WinForm之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#的新手,试图尽可能清楚地描述我的问题。


当项目启动时,Form1会自动显示,我在这里面有一行代码Program.cs

 Application.Run(new Form1()); 


我有另一种形式Form2,它不在Program.cs中,据说,Program.cs没有任何与Form2相关的代码


在Form1中,有一个按钮SwitchFormButton,其代码如下:

 Form2 f2 = new Form2(); 
f2.Show();

//this.Close();
//如果我在这里使用this.Close(),它实际上会关闭表单,结束应用程序,这不是我想要的。

//this.Visible = false;
//代码也不起作用,代码只是将Form1移到Form2后面,但两个表单仍然显示在窗口任务栏中。

这是我想要完成的事情:当我单击Form1按钮SwitchFormButton时,Form1应该关闭(也不会显示在任务栏中),Form2显示出来。


应该如何我这样做了吗?



谢谢。





解决方案

你好VAer8,


感谢您在此发布。


根据您的描述,您希望实现此功能,即单击Form1按钮SwitchFormButton时应关闭Form1并显示Form2。 / p>

您可以尝试以下代码。

 private void SwitchFormButton_Click(object sender,EventArgs e)
{
child c = new child();
this.Hide();
c.ShowDialog();
this.Close();
}

结果:



希望我的解决方案有所帮助。


祝你好运,


杰克







New to C#, trying to describe my question as clearly as I can.

Form1 is shown automatically when the project starts, I have a line of code like this inside Program.cs

Application.Run(new Form1 ());

I have another form Form2, which is NOT inside Program.cs, that being said, Program.cs does not have any code related to Form2

Inside Form1, there is a button SwitchFormButton, which has below code:

Form2 f2 = new Form2();
f2.Show();

//this.Close();
//If I use this.Close() here, it actually closes both form, end application, which is not what I want.

//this.Visible = false;
//The code does not work either, the code merely moves Form1 behind Form2, but both forms still show up in window taskbar.

Here is what I want to accomplish: when I click the Form1 button SwitchFormButton, Form1 should be closed (not showing up in task bar either), Form2 shows up.

How should I do it?

Thanks.


解决方案

Hi VAer8,

Thank you for posting here.

According to your description, you want to achieve this function that Form1 should be closed and Form2 shows up when you click the Form1 button SwitchFormButton.

You could try the following code.

private void SwitchFormButton_Click(object sender, EventArgs e)
        {
            child c = new child();
            this.Hide();
            c.ShowDialog();
            this.Close();
        }

Result:

Hope my solution could be helpful.

Best regards,

Jack



这篇关于如何在C#WinForm之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆