1

I want to open a form from another form with FormStartPosition.CenterParent. This works fine when having muliple screens and I run the program via Visual Studio. But when I run the EXE file it will actually not work.

var dialog = new Form();
dialog.StartPosition = FormStartPosition.CenterParent;
dialog.Show(this);

But when I run the EXE file (not via Visual Studio) and drag my form to the other screen and press the button that run the above code it will still open the form in the other screen.

I found a solution to open the forms at the same screen but there should be a way to always open a form in CenterParent in a easier way?

Here is the code snippet that will make the form always open at the same screen at least:

var dialog = new Form();   
dialog.StartPosition = FormStartPosition.Manual;
var currentScreen = Screen.FromControl(this);
dialog.Location = currentScreen.Bounds.Location;
dialog.Show(this);

Any ideas?

/Rob

Cyrix
  • 256
  • 1
  • 2
  • 17
  • 1
    `CenterParent` is meant for MDI child forms, it doesn't center the Form on its parent screen. What are you trying to do actually, open each Form on a different screen? – vgru Jun 16 '17 at 08:15
  • Ah, that actually explains things. No, Im trying to open the the sub form in the same screen that the form is placed on. I will right a solution for this now. Thanks – Cyrix Jun 16 '17 at 08:44

1 Answers1

0

See this post: https://stackoverflow.com/a/15862043/495455

You need to determine the size of the "virtual screen", which includes all monitors.

int screenLeft = SystemInformation.VirtualScreen.Left; 
int screenTop = SystemInformation.VirtualScreen.Top; 
int screenWidth = SystemInformation.VirtualScreen.Width; 
int screenHeight = SystemInformation.VirtualScreen.Height; 
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • How would this make it easier to open a form in CenterParent? :) – Cyrix Jun 16 '17 at 08:19
  • I think I've misunderstood, are you saying you actually don't have a problem, you're just looking for an easier way? – Jeremy Thompson Jun 16 '17 at 08:22
  • I was thinking you would just do a `this.Left = this.Parent.Width / 2 - this.Width / 2;` but using Location instead of Left... But that's what **CenterParent** does. Please confirm your question - I was the upvoter – Jeremy Thompson Jun 16 '17 at 08:25
  • The problem is that setting StartPosition = FormStartPosition.CenterParent dont actually work when having more than one screen when opening the program. I just want a easier solution and not a hack. Its working fine when running the program via visual studio, weird. It should just be to set that one property – Cyrix Jun 16 '17 at 08:31
  • CenterParent should open the form in center in its parent. But that wont work when having multiple monitors apperently – Cyrix Jun 16 '17 at 08:33