3

How to programmatically (assuming we've got a reference to it as a variable) bring a form already shown up to the very foreground and focus it in a C# WinForms application?

Ivan
  • 63,011
  • 101
  • 250
  • 382
  • possible duplicate of [What is the "right" way to bring a Windows Forms Application to the foreground?](http://stackoverflow.com/questions/1463417/what-is-the-right-way-to-bring-a-windows-forms-application-to-the-foreground) – nawfal Jan 05 '14 at 11:52

5 Answers5

7

You can use SetForegroundWindow. Good example here: C# Force Form Focus.

[DllImport("User32")]
private static extern int SetForegroundWindow(IntPtr hwnd);

Usage:

SetForegroundWindow(form.Handle);
Community
  • 1
  • 1
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
3

You should use the BringToFront() method

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Waleed
  • 3,105
  • 2
  • 24
  • 31
2

The answers here didn't quite do it for me. Using BringToFront wouldn't actually bring it to the front if the form was not focused and using Form.Activate just makes the form flash if it doesn't have focus. I wrote this little helper and it works flawlessly (I can't take total credit, found this somewhere on the web for WPF and converted it):

public static class FormHelper
    {
        const UInt32 SWP_NOSIZE = 0x0001;
        const UInt32 SWP_NOMOVE = 0x0002;
        const UInt32 SWP_SHOWWINDOW = 0x0040;

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        [DllImport("User32")]
        private static extern int SetForegroundWindow(IntPtr hwnd);

        [DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

        [DllImport("user32.dll")]
        private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

        public static void BringToFront(Form form)
        {
            var currentForegroundWindow = GetForegroundWindow();
            var thisWindowThreadId = GetWindowThreadProcessId(form.Handle, IntPtr.Zero);
            var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);
            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
            SetWindowPos(form.Handle, new IntPtr(0), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
            form.Show();
            form.Activate();
        }
    }

All you have to do is call FormHelper.BringToFront passing in the form you want to be shown.

Nick Olsen
  • 6,299
  • 11
  • 53
  • 75
0

Have you tried Form.Show() and/or Form.BringToFront() ?

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Yes. Doesn't help. In the case I've already shown a form and selected another window covering the window shown. After this invoking the .Show method does not bring the covered form up. – Ivan Jun 12 '11 at 01:34
  • 2
    @Ivan there is a `form.BringToFront()` that you can try but I'm not sure about it. – Bala R Jun 12 '11 at 01:35
  • r, yes, .BringToFront() is the answer. – Ivan Jun 12 '11 at 01:40
0
Form.Show();

or

Form.ShowDialog();

What's different? First show new form but all other will be activity. Second solution make that only this new form will be activity.

nirmus
  • 4,913
  • 9
  • 31
  • 50