2

If I want to find out the top Form in my WinForms application, or the one that was displayed last (no topmost flags used), can I use this:

Form topForm = System.Windows.Forms.Application.
    OpenForms[System.Windows.Forms.Application.OpenForms.Count - 1];
David
  • 15,894
  • 22
  • 55
  • 66
c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • http://stackoverflow.com/questions/1000847/how-to-get-the-handle-of-the-topmost-form-in-a-winform-app/9269744#9269744 – Lior Ohana Oct 21 '13 at 04:29
  • @LiorOhana: 'Focused' property is not set for the forms. I tried... – c00000fd Oct 21 '13 at 04:30
  • I think the accepted answer returns the topmost and not only the focused form. – Lior Ohana Oct 21 '13 at 04:42
  • @LiorOhana: Yeah, I saw it too. It's just too ugly for C#. I was thinking of something more elegant. The code I gave above works, I'm just not sure if it's reliable? – c00000fd Oct 21 '13 at 05:05
  • 1
    I don't think you can ssume the OpenForms array is ordered by the order of appearance. Do you think .NET reorder the array each time the appearance order changes? I'd be surprised if they did. – Lior Ohana Oct 21 '13 at 05:39

0 Answers0