How to show a form on current screen in C#?
Asked Answered
S

7

5

I want to show a new form in the same window from where it was invoked. I know a way to show this form on PrimaryScreen or Virtual Screen by code similar to as below:

MyForm.Location = Screen.PrimaryScreen.Bounds.Location;

But i want to show it on current screen. Is there a way to find out and show it on current screen?

Schrock answered 19/6, 2010 at 0:34 Comment(2)
Is this the main form of your application, or will it have an owner?Albite
Are you trying to replace the form shown with a new form? As if you were doing a wizard?Circumcise
L
7

I have done something like this to show my form centered to the current screen:

var screen = Screen.FromPoint(Cursor.Position);
myForm.StartPosition = FormStartPosition.Manual;
myForm.Left = screen.Bounds.Left + screen.Bounds.Width / 2 - myForm.Width / 2;
myForm.Top = screen.Bounds.Top + screen.Bounds.Height / 2 - myForm.Height / 2;
Lorraine answered 16/4, 2013 at 6:55 Comment(0)
D
5

You can use the same technique, but instead of using the PrimaryScreen, grab the screen using Screen.FromPoint and Cursor.Position:

Screen screen = Screen.FromPoint(Cursor.Position);
MyForm.Location = screen.Bounds.Location;
Disgrace answered 19/6, 2010 at 0:38 Comment(1)
I tried this. But still the new form is shown on the Primary Screen and the form size becomes a bit shorter. I tried to display if the screen where the cursor lies is a primary screen, and i get right answer. But when i assign the location, it doesn't get assigned properly.Schrock
B
1

It sounds like you aren't setting the StartPosition to Manual.

Bordello answered 30/3, 2011 at 14:55 Comment(0)
P
1

If you already have a parent form and want to open a new form on the same screen, give the ShowDialog method a reference to the parent form: newForm.ShowDialog(this); Without owner parameter ("this") the new form may open on the main screen even when your parent form is on another screen.

Parhelion answered 15/12, 2015 at 17:16 Comment(0)
A
1
  1. Click on the Form in design mode.
  2. Change the StartPosition property to CenterScreen .

This should open up the form on the active screen. Refer this for more values of StartPosition.

Adjustment answered 21/1, 2016 at 9:45 Comment(0)
D
1

I know this is late, but still, post my answer hope that it will help someone else. After several tries, I got this work with 3 monitors

var currentScreen = Screen.FromControl(this);
        if (!currentScreen.Primary)
        {
            var hCenter = currentScreen.Bounds.Left + (((currentScreen.Bounds.Right - currentScreen.Bounds.Left) / 2) - ((Width) / 2));

            var vCenter = (currentScreen.Bounds.Bottom / 2) - ((Height) / 2);
            StartPosition = FormStartPosition.Manual;
            Location = new Point(hCenter, vCenter);
        }
        else
        {
            CenterToScreen();
        }
Discontinuance answered 4/11, 2019 at 1:46 Comment(0)
A
1

Many years later, but no item was marked as the appropriate answer and I combined two comments to get it working (namely from Reed Copsey and Jason). I haven't tried any of the other methods described, since this worked without problem and got the Form to open on the monitor my cursor is at, as intended.

Here's my working code:

Screen screen = Screen.FromPoint(Cursor.Position);                
Application.Run(new Form1()
{ 
    StartPosition = FormStartPosition.Manual, //Summary in VS actually mentions this as needed to make use of Location
    Location = screen.Bounds.Location,
    WindowState = FormWindowState.Maximized
});
Atonsah answered 1/4, 2022 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.