How to center a modal Form on a specific Panel of a SplitContainer

1.2k Views Asked by At

I have GUI which is split in two pieces using a SplitContainer Control.
One part is a navigation Panel, the other a workspace Panel.

When I open the app, on start-up a new Form appears (using ShowDialog()), to welcomes Users. I would like to show it centered in the middle of the workspace Panel.

Is there anybody who knows how to solve this?

 Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     frmWelcome.ShowDialog()
 End Sub
2

There are 2 best solutions below

1
Jimi On BEST ANSWER

Assuming that Panel2 is your WorkSpace Panel, use its PointToScreen() method to calculate the Screen coordinates of frmWelcome and position it in the middle.

Be sure to set your frmWelcome.StartPosition = Manual, in the Designer or in its Constructor.

Here, I'm using the Shown event, to be sure that the pre-set positions in MainForm are already set.

Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Dim p As Point = New Point(
        ((SplitContainer1.Panel2.ClientSize.Width) \ 2) - frmWelcome.Width \ 2,
        ((SplitContainer1.Panel2.ClientSize.Height) \ 2) - frmWelcome.Height \ 2)

    frmWelcome.Location = SplitContainer1.Panel2.PointToScreen(p)
    frmWelcome.ShowDialog()
End Sub
0
Sasha On

You can use the properties on forms to do this.

set the frmWelcome form property StartPosition to CenterScreen.

If you want it center of the screen opening it, you will have to setup MDI but from there you can do frmWelcome.ShowDialog(Me) and set the property StartPosition to CenterParent.

Hope this helps!