0

I have two dialogs, FormA and FormB. I use the following code to show (modeless) FormB. The code is a button click executed from FormA.

    private void button_Click(object sender, EventArgs e)
    {
        FormB fB = new FormB();
        fB.Show(this); // FormA is the owner of FormB
    }

The problem is that when FormB is over FormA on the screen, if I click FormA, it is activated, but not brought to front. Actually FormB is always over FormA

Forms

Do you know why, and how to change this behavior, without remove the owner property?

NOTE: This is a simplification of my problem. In the real problem, FormA is a Windows Explorer window, and FormB is a managed WinForm, but the behavior is the same. If I do not pass the IWin32Window to Show(), it works fine, but If I close A, B is not closed and it does not respond to events (see the following entry).

Community
  • 1
  • 1
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219

3 Answers3

2

You cant do this without removing the owner property.

From Documentation: Owned forms are also never displayed behind their owner form.

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx

For your specific problem why do you not listen for the Close Event and then explicitly close your own form?

Skomski
  • 4,827
  • 21
  • 30
0

You can set TopMost property to true.

ARZ
  • 2,461
  • 3
  • 34
  • 56
0

A hack is to set WindowState = FormWindowState.Minimized in the OnDeactivate method of FormB (orverride it).

  protected override void OnDeactivate(EventArgs e)
  {
        base.OnDeactivate(e);
        this.WindowState = FormWindowState.Minimized;
  }

I don't know if that's what you would like.

Vijay Gill
  • 1,508
  • 1
  • 14
  • 16
  • But then Form2 is not over Form1. He can also automatic move Form1 etc but that's not want he want. – Skomski Aug 10 '11 at 09:39
  • That's what he said - Form1 must be brought in front, so the only way I could think of was to minimise the Form2 in his given scenario. Also I put a disclaimer at the end of my answer :) – Vijay Gill Aug 10 '11 at 09:42