Click here to Skip to main content
13,295,051 members (85,677 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Please I need to control my extended monitor and to show two different application one on main and another on the external monitor, how can i do it using c# code I need to show specific window on the main , and another one on the extended thank you in advanced
Posted 11-Nov-12 5:25am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi there.

This is an extension method to move a form onto the first non-primary screen.
It returns a Nullable<bool> to tell whether the form have been moved, not moved or an error did occur.

namespace System.Windows.Forms
    public static class FormExtensions
        public static bool? SetScreenToFirstNonPrimary(this Form self)
                // Retrieves the collection of available screens (monitors)
                var aScreens = Screen.AllScreens;
                // If count is not greater than 1 then exit
                if (aScreens.Length <= 1)
                    return false; // screen kept original
                // Saves current screen reference
                var screenOld = Screen.FromControl(self);
                foreach (var screen in aScreens)
                    // Skips primary and current screen
                    if (screen.Primary || screen.Equals(screenOld))
                    var boundsScreen = screen.Bounds;
                    var oldState = self.WindowState;
                    // If form is currently maximized ...
                    if (oldState == FormWindowState.Maximized)
                        self.WindowState = FormWindowState.Normal;
                        self.StartPosition = FormStartPosition.Manual;
                        self.Location = boundsScreen.Location;
                        self.WindowState = FormWindowState.Maximized;
                        self.StartPosition = FormStartPosition.Manual;
                        // Center into new screen
			var sizeGaps = boundsScreen.Size - self.Size;
			var x = boundsScreen.Left + (sizeGaps.Width / 2);
			var y = boundsScreen.Top + (sizeGaps.Height / 2);
                        self.Location = new Point(x, y);
                    return true; // screen has been changed
                return false; // screen kept original
            catch (Exception ex)
                // process exception ex
                // ...
                return null; // error occurred

You can call it from the form's constructor just after InitializeComponent(); as

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171207.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100