按钮打开一个新表单并关闭自己 [英] Button to open a new form and close itself

查看:79
本文介绍了按钮打开一个新表单并关闭自己的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个按钮并写了;



I added a button and wrote;

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MainWindow_1 mw = new MainWindow_1();
            MainWindow mw1 = new MainWindow();
            mw1.Show();
            mw.Hide();
//mw.Close(); doesnt work as well//
}



由此我打开一个新的表格,但是mw并没有关闭。



我该怎么做?


by this i open a new form but mw doesn''t close.

How can i do that?

推荐答案

你不是想隐藏任何东西。窗口 mv 尚未显示,因此隐藏它无效。此外,隐藏不是关闭。如果由于某种原因,你看到一些窗口(除了 mw1 )仍在显示,这是一些其他窗口,它与隐形窗口无关 mv ,也许你是真正的主窗口。



顺便说一句,如果一个窗口类被调用 MainWindow ,这并不意味着它真的是主要的。主窗口是用作 Application.Run(Window)的参数的窗口 http://msdn.microsoft.com/en-us/library/ms597011.aspx [ ^ ]。



如果创建WPF应用程序是通常的 方式,通过Visual Studio模板,这个对 Application.Run 的调用是自动生成的,所以你可能看不到它,但它总是存在于你的应用程序中。 />


请不要问你应该怎么做:你不应该做任何像你的代码所写的内容;它没有任何意义。特别是,你不应该关闭一个主窗口,它会关闭应用程序。



调用它的方法之一 Application.Run 几次,每次都有不同的窗口,但我不认为你真的需要它。您应该更好地考虑您的应用程序设计。



-SA
You are not trying to hide anything. The Window mv is not yet shown, so hiding it has no effect. Also, hiding is not closing. If, by some reason, you see some window (except mw1) which is still showing, this is some other window, which has nothing to do with invisible window mv, maybe you real main window.

By the way, if a window class is called MainWindow, it does not mean it is really main. The main window is the one used as a parameter of Application.Run(Window), http://msdn.microsoft.com/en-us/library/ms597011.aspx[^].

If create your WPF application is a "usual" way, through the Visual Studio template, this call to Application.Run is auto-generated, so you may not see it, but it always exists in your application.

And please don''t ask what should you do: you shouldn''t do anything like what is written in your code; it just makes no sense. In particular, you should not close a main window, it would close the application.

One of the approaches it to call Application.Run several times, each time with a different window, but I don''t think you really need it. You should better think on your application design.

—SA





如果打开任何窗口或任何正在运行的应用程序类似于该过程。您可以停止进程或终止负责打开窗口的进程对象。通过这种方式,您可以关闭要关闭的窗口。



谢谢
Hi,

If any window is opened or any application is running that resembles to the process. You can stop the process or kill the process object that is responsible for opening the window. In this way you can able to close the window that you want to close.

Thanks


这篇关于按钮打开一个新表单并关闭自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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