当用户在表单上点击X按钮时,我应该如何隐藏它而不是关闭它?
我已经尝试在
FormClosing
中使用this.hide()
,但它仍然会关闭表单。就像这样:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
(通过Tim Huffman提供)
我之前已经在另一个回答中留言,但想自己提供一下我的看法。基于你的问题,这段代码与顶部答案类似,但增加了另一个功能。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
如果用户只是点击窗口中的X,表单会隐藏;如果使用任务管理器、Application.Exit()
或Windows关闭,表单将被正确关闭,因为return
语句将被执行。 protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
使用OnFormClosing方法也可让派生类在不附加委托的情况下处理事件。这是在派生类中处理事件的首选技术。
如果你想使用显示/隐藏的方法,我其实已经为最近完成的一个游戏的菜单结构做过这个。这是我做的:
创建一个按钮,并为你想要完成的操作编写代码,例如“下一页”按钮,在此示例中,代码如下:
btnNext.Enabled = true; //This enabled the button obviously
this.Hide(); //Here is where the hiding of the form will happen when the button is clicked
Form newForm = new newForm(); //This creates a new instance object for the new form
CurrentForm.Hide(); //This hides the current form where you placed the button.
private void btnInst_Click(object sender, EventArgs e)
{
btnInst.Enabled = true; //Enables the button to work
this.Hide(); // Hides the current form
Form Instructions = new Instructions(); //Instantiates a new instance form object
Instructions.Show(); //Shows the instance form created above
}
因此,有一个显示/隐藏方法可以用几行代码实现,而不是为一个简单的任务编写大量的代码。我希望这能帮助解决你的问题。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
StackTrace trace = new StackTrace();
StackFrame frame;
bool bFoundExitCommand = false;
for (int i = 0; i < trace.FrameCount; i++)
{
frame = trace.GetFrame(i);
string methodName = frame.GetMethod().Name;
if (methodName == "miExit_Click")
{
bFoundExitCommand = true;
Log("FormClosing: Found Exit Command ({0}) - will allow exit", LogUtilityLevel.Debug3, methodName);
}
if (methodName == "PeekMessage")
{
bFoundExitCommand = true;
Log("FormClosing: Found System Shutdown ({0}) - will allow exit", LogUtilityLevel.Debug3, methodName);
}
Log("FormClosing: frame.GetMethod().Name = {0}", LogUtilityLevel.Debug4, methodName);
}
if (!bFoundExitCommand)
{
e.Cancel = true;
this.Visible = false;
}
else
{
this.Visible = false;
}
}
form.ShowDialog()
时,你在“请求”这种行为。原因是,form.ShowDialog 不会返回,直到表单被隐藏或销毁。因此,当表单被隐藏时,form.ShowDialog 中的消息循环将其销毁以便返回。form.Show()
立即返回,你可以随意显示和隐藏此窗口,它不会被销毁,直到你显式地销毁它。Application.Run
或 Application.DoEvents
在循环中运行消息循环。如果创建表单的线程退出,则该表单将被销毁。如果该线程不运行消息循环,则它拥有的表单将无响应。ApplicationContext
的设计目的之一。详情请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx
基本上,你需要从 ApplicationContext 派生一个类,将你的 ApplicationContext 实例作为参数传递给 Application.Run()
。// Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit.
MyApplicationContext context = new MyApplicationContext();
// Run the application with the specific context.
Application.Run(context);
您的应用程序上下文需要知道何时可以退出应用程序,以及隐藏表单时不应退出应用程序。当应用程序退出时,您的应用程序上下文或表单可以调用应用程序上下文的ExitThread()
方法来终止消息循环。此时,Application.Run()
将返回。
如果不了解您的表单层次结构和决定何时隐藏表单何时退出的规则,就无法更具体地说明。
Application.Run(MyAppContext)
。 - John Knoeller
e.CloseReason
(请查看另一个答案)。否则,在系统关闭或其他事件关闭表单时,表单将无法关闭。 - SQL Police