我的项目中有两个表单:Form1和Form2.Form1中有一个按钮,我想要做的是关闭Form1并在单击该按钮时显示Form2.
首先,我试过了
Form2 frm = new Form2();
frm.Show();
this.Close();
Run Code Online (Sandbox Code Playgroud)
但随着Form1的关闭,Form2也被关闭了.接下来,我试过了
Form2 frm = new Form2();
frm.Show();
this.Hide();
Run Code Online (Sandbox Code Playgroud)
但是有一个缺点是当Form2关闭时应用程序不会退出.因此,我必须在Form2的form_FormClosing部分添加其他源代码.
嗯......我想知道这是否是正确的方法......那么,处理这个问题的正确方法是什么?

我有不同的外观中存在的问题Blend/VS2012 IDE和Debugging.在IDE,Rectangle它位于中心,但在编译或调试时,边距的大小是不同的.在我看来,这是因为窗口边框的大小不同.我真的想解决这个问题.有什么建议?
谢谢.
XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="202" Width="194">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="151" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="166"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
编辑XAML代码:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="202" Width="194">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="151" Margin="10" Stroke="Black" VerticalAlignment="Center" Width="166"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
结果是一样的.这是我的电脑的问题吗?