在 c# 中旋转的 Windows 窗体 [英] Windows forms spinning in c#
问题描述
我有两个窗体.现在我必须关闭第一个窗体并显示第二个窗体,反之亦然.我该怎么做.我将此指针传递给第二个窗体的构造函数,然后尝试关闭它,但这不起作用.我不能在这里使用 showdialog.
I have two windows forms.Now I have to close first one and show the second form and vice-versa.How can i do it.I was passing this pointer to the constructor of second form and then trying to close it,but this did not work.I can not use showdialog here.
推荐答案
为 Program 类中的每个表单添加静态变量:
Add static variables to each form in the Program class:
static class Program
{
public static Form1 f1=null;
public static Form2 f2 = null;
public static FullClose=false;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
f1=new Form1();
f2 = new Form2();
Application.Run(f1);
}
}
然后在每个表单的 Form_Closing 事件中:
Then in the Form_Closing event of each form:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Program.FullClose==false)
{
e.Cancel = true;
this.Visible = false;
Program.f2.Show();
}
}
(在 Form2 中将 Program.f2.Show()
更改为 Program.f1.Show()
).
(Change Program.f2.Show()
to Program.f1.Show()
in Form2).
当然,这会阻止应用程序关闭,因此您应该提供一个额外的按钮(或类似按钮)来设置一个布尔静态变量 (FullClose),Form_Closing 事件可以检查它们是否应该正确关闭.
Of course this will stop the application from ever closing, so you should provide an extra button (or similar) that sets a boolean static variable (FullClose) that the Form_Closing events can check to see if they should properly close or not.
这篇关于在 c# 中旋转的 Windows 窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!