-2

I have got multiple forms open. Now say I have forms 1,2 and 3 are open. 1 opened 2 and 2 opened 3. Now when user clicks on 2, this screen should come to the front and send screen 2 to the back of it. Likewise whenever a screen is being clicked, that screen should come to the front and the screen displayed in the front should go one screen back.

I tried various things in activated and deactivated event but none worked as in the following:

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

protected override void OnActivated(EventArgs e)
{
  base.OnActivated(e);
  this.WindowState = FormWindowState.Normal;
  closeEvent = false;
}

Any help on this will be much appreciated.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Ajay
  • 17
  • 1
  • 5

3 Answers3

1

From what I have used in my WinForms application, I would use the following:

this.Focus(); //We always focuss on it because it make on the top..

This applies to form controls in general. Make a notice that if a modal window is present, this will affect the outcome of this command.

Activate is similar to focus but does not bring it to the top but more so flashes the application to the task bar indicating that it is active.

Let me know if this works. :)

  • 1
    Thanks for the responses. I put this in the above code I discussed but it doesn't bring any difference when I click on the window which I want to bring to the front. What I am trying to do is bring the window whatever the user clicks to the front. Surprised that Microsoft doesn't take care of this!! Also I don't know how this applies here: Owned forms are also never displayed behind their owner form from the following: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.ownedforms.aspx – Ajay Jul 21 '14 at 13:30
  • By default, this should be the case. How are you displaying the forms? What command are you using? – sunnysidedown916 Jul 21 '14 at 17:04
  • one by f.WindowState = FormWindowState.Normal; f.BringToFront(); f.Focus(); return (InheritedFrm)f; and others by frm.Show(interface form) – Ajay Jul 21 '14 at 18:59
  • Take a look at this: http://stackoverflow.com/questions/2618073/bring-winforms-control-to-front?rq=1 – sunnysidedown916 Jul 21 '14 at 21:54
  • Or this: http://stackoverflow.com/questions/2013849/cant-consistently-bring-form-to-front?rq=1 – sunnysidedown916 Jul 21 '14 at 21:54
1

To bring a winforms form to the front (topmost) you can try below:

yourForm.Activate()

this activates and gives it focus.

See this link.

Hope it helps!

Willy
  • 9,848
  • 22
  • 141
  • 284
0

I think Control.BringToFront() should help. If I remember correctly, Form does inherit from Control.

Although I'm not really sure I understand why it doesn't already have this behavior. If I'm not misunderstanding, you want to bring the window that the user clicks on to the front, right? Doesn't Windows do that?

Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54