English 中文(简体)
C# Form Problem: new form losing control and randomly hiding
原标题:

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
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签