vb.net - Opening winforms in the same screen


Keywords:vb.net 


Question: 

I have an application with multiple win forms. I have noticed is that if the user has multiple screen displays and changes the application to one screen, the other forms that are called by other buttons, will open in the main display and not where my main application or form is.

How can I change this?


1 Answer: 

You can use the Form.CenterToParent Method on your Forms, they will then open up centered on your the creating Form, Or you can use the Screen Class to get the Bounds of the Display that your Main application is running on, then pass it to your created forms.

Edit:

On second thought assigning the Owner might just be enough, I don't have a dual monitor computer booted up at this time to test though

Dim frm As Form1 = New Form1()
frm.Owner = Me
frm.CenterToParent()
frm.Show()

Edit

Just had a chance to check it out. It was as I thought assigning the Owner to the new Form or using the Form.Show(IWin32Window) will open the new Form on the same screen as the originating Form.

frm.Show(Me)


Looks like the CenterToParent property is protected now. According to the MSDN link

Do not call the CenterToParent method directly from your code. Instead, set the StartPosition property to CenterParent. If the form or dialog is top-level, then CenterToParent centers the form with respect to the screen or desktop