简体   繁体   中英

How to completely bring to front a WinForm when activated

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

形式

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 ).

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?

You can set TopMost property to true.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM