C#表单问题:失去控制权和随机隐藏的新形式 [英] C# Form Problem: new form losing control and randomly hiding

查看:168
本文介绍了C#表单问题:失去控制权和随机隐藏的新形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#3.5应用程序中遇到了表单奇怪的行为。点击按钮后,我的form1隐藏自身,创建一个新的form2,并显示form2。 Form1还包含form2关闭时触发的事件方法。这里是Form1中的代码:

  Form2 form2; 

void button1_Click(object sender,EventArgs e)
{
this.Hide();
form2 = new form2();
form2.Show();
form2.FormClosed + = new FormClosedEventHandler(form2_FormClosed);
}

void form2_FormClosed(Object sender,FormClosedEventArgs e)
{
form2.Dispose();
form2 = null;
this.Show();
}

现在,我的问题是有时当我打开form2(隐藏form1)时,或者当我关闭form2(显示form1)时,新窗体将出现在屏幕上闪烁,然后隐藏自身。它仍然是开放的,我可以从任务栏点击它再次显示,但窗口本身发送到任何其他打开的窗口后面。它看起来像打开了,但立即最小化。

这种行为是随机发生的。有时候表格会被打开并隐藏起来没有问题,但有时他们会失去对另一个窗口的关注。我曾尝试使用focus(),activate()和最上层,但都未能阻止突然隐藏。

有人知道为什么会发生这种情况,以及如何修正它?



谢谢。

解决方案

不久。在短时间内,您的应用程序没有可以包含焦点的窗口。这迫使Windows去寻找另一个窗口来关注它,它会从另一个应用程序中选择一个窗口。该窗口现在将成为前景窗口,您的第二个窗体将不会获得焦点并且会以Z顺序显示较低。修复很简单:

  void button1_Click(object sender,EventArgs e)
{
form2 = new窗口2();
form2.Show();
form2.FormClosed + = new FormClosedEventHandler(form2_FormClosed);
this.Hide(); // Moved
}


I'm encountering strange behavior with forms on a c# 3.5 app. On a button click, my form1 hides itself, creates a new form2, and shows form2. Form1 also contains the event method triggered when form2 closes. Here's the code inside Form1:

Form2 form2;

void button1_Click(object sender, EventArgs e)
    {           
        this.Hide();
        form2 = new form2();
        form2.Show();
        form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);               
    }

void form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        form2.Dispose();
        form2 = null; 
        this.Show();  
    }

Now, my problem is that sometimes when I open form2 (hiding form1), or when I close form2 (showing form1), the new form will come up on the screen for a blink and then hide itself. It's still open and I can click it from the taskbar to show it again, but the window itself is sent behind any other open windows. It looks like it opens up but minimizes instantly.

This behavior occurs randomly. Sometimes forms will open up and hide without a problem, but sometimes they'll lose focus over another window. I've tried using focus(), activate(), and topmost but all have failed to prevent the sudden hiding.

Does anyone know why is this happening and how to fix it?

Thanks.

解决方案

You hide your form too soon. For a brief moment, your app has no window that can contain the focus. That forces Windows to go hunting for another window to give the focus to, it will pick one from another application. That window will now be the foreground window, your second form will not get the focus and appear lower in the Z-order. The fix is simple:

void button1_Click(object sender, EventArgs e)
{           
    form2 = new form2();
    form2.Show();
    form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);               
    this.Hide();  // Moved
}

这篇关于C#表单问题:失去控制权和随机隐藏的新形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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